% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% This is a simple template for a LaTeX document using the "article" class.
% See "book", "report", "letter" for other types of documet.

\documentclass[11pt]{book} % use larger type; default would be 10pt
 \usepackage{amsmath, amsthm, amssymb}
\input{amssym.def}
\input{amssym.tex}
\font\teneufm=eufm10
\font\seveneufm=eufm7
\font\fiveeufm=eufm5
\newfam\eufmfam
\textfont\eufmfam\teneufm
\scriptfont\eufmfam\seveneufm
\scriptscriptfont\eufmfam\fiveeufm
\def\eufm#1{{\fam\eufmfam\relax#1}}
\usepackage[dvipsnames]{color}
\definecolor{mygreen}{rgb}{0.0,0.43,0.07}
\definecolor{darkgreen}{rgb}{0.0,0.3,0.0}

\definecolor{mydone}{rgb}{0.0,0.43,1.0}
\definecolor{.}{rgb}{0.0,0.43,1.0}
\usepackage[T1]{fontenc}   % so _, <, and > print correctly in text.
\usepackage[strings]{underscore}    % to use "_" in text
\usepackage[pdftex,colorlinks=true]{hyperref}   % Must be last package!
%\usepackage{hyperref}
 
\usepackage[titletoc]{appendix}





\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)

%%% Examples of Article customizations
% These packages are optional, depending whether you want the features they provide.
% See the LaTeX Companion or other references for full information.

%%% PAGE DIMENSIONS
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper} % or letterpaper (US) or a5paper or....
% \geometry{margins=2in} % for example, change the margins to 2 inches all round
% \geometry{landscape} % set up the page for landscape
%   read geometry.pdf for detailed page layout information

\usepackage{graphicx} % support the \includegraphics command and options

% \usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent

%%% PACKAGES
\usepackage{booktabs} % for much better looking tables
\usepackage{array} % for better arrays (eg matrices) in maths
\usepackage{paralist} % very flexible & customisable lists (eg. enumerate/itemize, etc.)
\usepackage{verbatim} % adds environment for commenting out blocks of text & for better verbatim
\usepackage{subfig} % make it possible to include more than one captioned figure/table in a single float
% These packages are all incorporated in the memoir class to one degree or another...

%%% HEADERS & FOOTERS
\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\pagestyle{fancy} % options: empty , plain , fancy
\renewcommand{\headrulewidth}{0pt} % customise the layout...
\lhead{}\chead{}\rhead{}
\lfoot{}\cfoot{\thepage}\rfoot{}

%%% SECTION TITLE APPEARANCE
\usepackage{sectsty}
\allsectionsfont{\sffamily\mdseries\upshape} % (See the fntguide.pdf for font help)
% (This matches ConTeXt defaults)

%%% ToC (table of contents) APPEARANCE
\usepackage[nottoc,notlof,notlot]{tocbibind} % Put the bibliography in the ToC
\usepackage[titles,subfigure]{tocloft} % Alter the style of the Table of Contents
\renewcommand{\cftsecfont}{\rmfamily\mdseries\upshape}
\renewcommand{\cftsecpagefont}{\rmfamily\mdseries\upshape} % No bold!
\def\xxxx{xxxx}
%%% END Article customizations
\def\eq#1{Eq.~(\ref{#1})}
\def\Eq#1{Eq.~(\ref{#1})}
\def\eqs#1{Eqs.~(\ref{#1})}
\def\eqe#1{(\ref{#1})}
\def\sec#1{Sec.~(\ref{#1})}
\def\secs#1{Secs.~(\ref{#1})}
\def\sece#1{(\ref{#1})}
\def\Fig#1{Figure {\rm \ref{#1}}}
\def\fig#1{Figure {\rm \ref{#1}}}
\def\fige#1{ {\rm \ref{#1}}}
\def\figs#1{Figures {\rm \ref{#1}}}
\def\chas#1{Chapters {\rm \ref{#1}}}
\def\Chas#1{Chapters {\rm \ref{#1}}}
\def\app#1{Appendix {\rm \ref{#1}}}
\def\cha#1{Chapter {\rm \ref{#1}}}
\def\Cha#1{Chapter {\rm \ref{#1}}}
%%% The "real" document content comes below...
\def\labelf{15}
\def\beginbook{\input \jobname.label \immediate\openout\labelf=\jobname.label \begin{document}}
\def\labelfig#1{\addtocounter{figure}{1}\immediate\write\labelf{\noexpand\newlabel{#1}{{\thefigure}{\thepage}}}}
\def\endbook{\immediate\closeout\labelf \end{document}}
%\font\tencafm=Arialr at 9.0pt                      
\def\hel{\rm }
\def\vskips{\vskip 0.5cm }
\def\v5{\vskip 0.5cm }
\def\sk{\vskip 0.3cm}
\def\locp{example_programs/}
\def\loct{build_my_own_tiny_package/my_analysis_tools/}
\input{path}

\def\htmte#1{\href{ #1}{\color{blue}\bf \it  #1}}
\def\htmt#1{\href{\loct #1}{\color{blue}\bf \it \loct #1}}
\def\htm#1{\href{\locp #1}{\color{mygreen}\bf \it \locp #1}}
%\def\htmt#1#2{\href{\loct #1}{\color{blue}\bf \it \loct #2}}
%\def\htm#1#2{\href{\locp #1}{\color{mygreen}\bf \it \locp #2}}

\renewcommand\Re{\operatorname{Re}}
\renewcommand\Im{\operatorname{Im}}


\title{Accelerator  Perturbation Theory  \\ with examples based FPP and PTC\\ with the inclusion of new Complex FPP}

%\author{ \'Etienne Forest \\ edited by Piotr Skowronski and who ever wants to contribute} % 
\author{E.F. } % 
\date{ now} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed 

%\begin{document}
\beginbook
\maketitle


\tableofcontents


\chapter{\color{.} Preface: why this book and how to read it}


\footnote{The masculine gender applies to females, males and anyone in between unless otherwise specified.}

\noindent \fbox{\vbox{ \footnotesize
{\noindent \bf \color{red}How to read this book:}
the reader should identify in the table of content some object he would like to compute, look at the algorithm and then, if interested, look at the theoretical explanation. 
The theoretical explanations, {\it \color{red} in the body of this document}, will assume that the appropriate normal form has been performed. I will not show ``how'' we do the normal form in the actual code. The reader who wants to go that far will need to consult references or figure it out on his own. However, some ``advanced'' material is available in the appendix. For example, the numerical implementation of the normal form in the small toy package at \htmte{build_my_own_tiny_package} is explained in \app{apptiny}. This is a mathematically correct   numerical theory in one degree of freedom (1-d-f) including nonlinearities. In \app{chapgf},  I descend into pure mathematical  hell when I compare the analytical calculation corresponding to the FPP calculation of \sec{secdispgen} with the hellish theory of Guignard so loved by accelerator physicists. In the simplest of cases, agreement of Guignard's theory with an FPP calculation entails summing infinite sums. Please do not feel compelled to read these appendices.

So in summary,
\begin{itemize}
\item First look at the example programs. 
\item Then try to see how the mere existence of a normal form (who cares how it is computed!) facilitates these calculations. 
\item Then go to \app{apptiny} and the toy package. 
\item And, if truly interested in the theory and in obscenities, look at \app{chapgf} and references.
\end{itemize}

 {\color{red} I must point out that looking at FPP itself is not very helpful in teaching the theory because of the lack of a complex TPSA; the tiny toy package is more useful.  A future  FPP will most likely be in C++ and handle complex TPSA. }
 
The practical examples are baits attached to hooks that will perhaps allow me to drag  the readers into the world of normal forms on the map which is the natural extension of the Courant-Snyder theory applied to matrices.
}
}

\vskips

The topic of normal form on the map is an integral part of the code PTC. With the advent of
truncated power series algebra (TPSA)\footnote{Berz's ``DA'' for example}, it seems almost criminal that these tools are not readily available and understood by the average users of a code like MAD-X or BMAD. 

Normal form techniques applied on a Taylor maps  allow one unified theory and one unified ``Twiss'' algorithm that can include 

\renewcommand{\theenumi}{\arabic{enumi}}

\begin{enumerate}
\item  orbital dynamics 
\item  spin
\item  radiation 
\item  slow strength modulation of magnets
\item photon fluctuation (synchrotron ``integrals'' theory)
\end{enumerate}
 
One resonance normal form can also be written for


\begin{enumerate}
\setcounter{enumi}{5}
\item  orbital dynamics 
\item  spin
\item   system with friction to compute limit cycles
\end{enumerate}


Thus the question arises: how can I  effectively transmit this knowledge to practising accelerator physicists. I  have been favouring a top-down approach: if one understands normal form theory taking a view from above--- in the rarefied air of mathematics--- then all should be simple. Unfortunately, this does not seem to work for most people: apparently the rarefied air of mathematics induces asphyxia and brain death in most accelerator practitioners! So this manual will start from the bottom of the mountain and encourage the reader to climb Sinai until enough of the promise land will be in view.

\vskips

I will start with a simple fact which apparently I did not have the brains to  realise:  {\it most people use a  code to compute something familiar.}
 They then look in the manual to see if it is encoded. If it is encoded, they try to use the code. A small minority of the users will enquire on the techniques used in the code:  they will then program it  through some script language if available or directly in the source. Then an even tinier minority will actually learn the mathematics.  


This pedagogical approach assumes the existence of a code (or library):  it will be    PTC and its analysis library FPP. 

\vskips

We use the FPP/PTC library because
\begin{enumerate}
\renewcommand{\theenumi}{\Alph{enumi}}
\item   it is a (symplectic) integrator, 
\item  it has radiation,
\item  it has spin,
\item it has a rather complete analysis package,
\item it is included in MAD-X and BMAD used respectively at CERN and Cornell.
\end{enumerate}

I do not view  FPP/PTC  as the ultimate and ideal realization of our theoretical dreams. For example, an analysis package based on a complex TPSA could be abstracted on the computer in a way far closer to the theory: phasors are complex. Unfortunately FPP is based on an old fortran77 code which  only handle Taylor series of real numbers. In the not distant future, there is hope that a new FPP and PTC will exist, as part of MAD perhaps. This new package will have a script attach to it which will allow the user to fully exploit normal form theory and even {\it write} his own normal form in the script file. At present I can only give you a small toy package in one degree of freedom  if you want to see a complex TPSA/FPP/Tracker in action.


\noindent \fbox{\vbox{ 
\noindent \color{red} The algorithms you will learn here are {\bf  universal}. Therefore you do not waste your time looking at the present PTC examples or the toy package  even if your ultimate tool is  a``new MAD'' or someone else's software.
Go ahead and try the examples. Modify them, molest them, mess them up and pass them along.
}
}



\chapter{What is a tracking code: PTC used as example}\label{chap0}

\section{Why do we use ``lenses'' and not time tracking?} 
\subsection{Concept of a Beam Line}
\subsection{Patches: Translations and Rotations}
\section{Explicit Symplectic Integration with examples} 
\subsection{Quadrupole} 
\subsection{Dipoles} 
\subsection{Rectangular Bends}
\subsection{Pill Box Cavities}
\subsection{Non-Symplectic Elements}
 
\chapter{The ray and its extensions}\label{chap1}

\section{What can we compute besides the ray  itself?} 
\subsection{Extend the ray by adding physics: spin and stochastic envelopes} 
\subsection{Extend the ray by  adding useful mathematics:   Taylor series and maps for beam lines} 

\section{FPP: A Polymorphic Taylor Type } 
\subsection{ Maps for the Orbital Dynamics} 
\subsection{ Maps for the Spin Dynamics} 
\subsection{ Maps with Parameter Dependence} 



\chapter{Analysis in FPP/PTC: Normal Form for Invariants}\label{chanorminv}



\section{\color{.} A bit of theory to show the usefulness of normal forms: invariants}\label{secampcsi}


How is it computed? At this stage, who cares!
The important point is  that you have these maps from a reliable black box library.

The relevance of a normal form is immediately apparent in rings. We are interested the  stability of the one-turn map $m$:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!$1^["! ^$^""*|:"x^n_^$^p^n_}}:!,F,]:"m<c!$1($'b M m<c!$1("|
%|m<c!$1^["! ^$^x^0_^$^p^0_}}}}}},]$^m_^n<c!$1^["! ^$^x^0_^$^p^|
%|0_}}:! : ;8/<:";bP8mapn: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left({\begin{matrix}{x}_{n}\\
{p}_{n}\end{matrix}}\right)&=m\left({\cdots\ m\left({m\left({\begin{matrix}{x}_{0}\\
{p}_{0}\end{matrix}}\right)}\right)}\right)=
{m}^{n}\left({\begin{matrix}{x}_{0}\\
{p}_{0}\end{matrix}}\right) \label{mapn}\end{align}
%
In other words, can we  say something about the trajectory as $n$ gets large. 
Now suppose the following is true\footnote{I will repeat this story in every sections so you do not have to jump back and forth. Skip it if you have read it. See \eq{nfave} for example.}:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"m:!,F,]"#Times|:#a"$Symbol^:$&c0  .P:"&c0!*r:$&c0  .P$^|
%|:"&c0!*a_(",M1}:! : ;8/<:";bP8nfavecs: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} m&=a\circ r\circ {a}^{-1} \label{nfavecs}\end{align}
The map $a$ is some horrible transformation and $r$ is an amplitude dependent rotation. 
Amplitude dependent rotations, in 1-d-f, have the following form:
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"r<c!$1^["! ^$^z^1_^$^z^2_}}:!,F,] <c!$1^["" ($-<cos"#Symbol^:#&c0  q|
%|<c!$1^:"&c0!*J}}($:!-<sin:#&c0  q<c!$1^:"&c0!*J}}(%,M:!-<sin:#&c0  q|
%|<c!$1^:"&c0!*J}}($:!-<cos:#&c0  q<c!$1^:"&c0!*J}}}}<c!$1^["! |
%|^$^z^1_^$^z^2_}}"$Monaco^:$  where :"2J,]$^z^1^2,K$^z^2^2: ;8/<|
%|:";bP8rotavecs: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} r\left({\begin{matrix}{z}_{1}\\
{z}_{2}\end{matrix}}\right)&= \left({\begin{matrix}\cos\theta \left({J}\right)&\sin\theta \left({J}\right)\\
-\sin\theta \left({J}\right)&\cos\theta \left({J}\right)\end{matrix}}\right)\left({\begin{matrix}{z}_{1}\\
{z}_{2}\end{matrix}}\right)~~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~2J=
{z}_{1}^{2}+{z}_{2}^{2}\label{rotavecs}\end{align}
One notices that \eq{rotavecs} is not a linear map because the angle $\theta $ is a function of the amplitude $J$.

The map $r$ is obviously stable. But it is more important to realize that its invariants are obvious: circles in the $(z_1,z_2)$ variables. Given this fact, we can immediately construct the invariants of $m$
by expressing the normalised coordinates $\vec z$ in terms of the original phase space, .i.e.,
%
%]|Expr|[#b @`b___})5# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  e^"#*|:#&c0!*k_:"&c0  :!&c0!*,F:#,]2$^J^k_,]|
%|$^z($2k,M1}^2,K$^z("2k}^2: ;bP;/":!;bP8 ,F,]$^<c!$1(&$^:#z($2|
%|k,M1}_,H<c$%!^x},L<c$%!^p},I}}_^2 ,K$^<c!$1(&$^z("2k}_,H<c$%!|
%|^x},L<c$%!^p},I}}_^2:! : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\varepsilon }_{k}&=
2{J}_{k}=
{z}_{2k-1}^{2}+{z}_{2k}^{2}\nonumber \\
 &={\left({{z}_{2k-1}(\vec{x},\vec{p})}\right)}^{2}\ +{\left({{z}_{2k}(\vec{x},\vec{p})}\right)}^{2} \end{align}
which needs simply the map $a^{-1}$. 
%
%]|Expr|[#b @`b___})3# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  e^"#*|:#&c0!*k_:"&c0  :#&c0!*,H<c$%!^x},L<c$%!|
%|^p},I:!,F,]$^<c!$1)&# b'4$^:#a($2k,M1}(",M1},H<c$%!^x},L<c$%!|
%|^p},I}& b!( b"0 b#8 b$@ b%H b&P!WW}}_^2 ,K$^<c!$1)&# b'4$^a("|
%|2k}(",M1},H<c$%!^x},L<c$%!^p},I}& b!( b"0 b#8 b$@ b%H b&P!WW}}|
%|_^2:! : ;8/<:#;bP8csinv: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\varepsilon }_{k}(\vec{x},\vec{p})&={\left({{a}_{2k-1}^{-1}(\vec{x},\vec{p})}\right)}^{2}\ +{\left({{a}_{2k}^{-1}(\vec{x},\vec{p})}\right)}^{2} \label{csinv}\end{align}


\section{\color{.}Courant-Snyder: The quadratic invariants in one or more degrees of freedom}\label{seccs}

In the linear 1-d-f case, \eq{csinv} gives us the famous Courant-Snyder invariant. For example, suppose that $a^{-1}$ is a linear map represented by a matrix $A^{-1}$.
%
%]|Expr|[#b @`b___})+# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"A_(",M1}:!,F,]<c!$1^[$$ ^$^A(!11}(",M1}^$^A(!12}(",M1}|
%|^$^A(!13}(",M1}^$^A(!14}(",M1}^$^A(!21}(",M1}^$^A(!22}(",M1}^|
%|$^A(!23}(",M1}^$^A(!24}(",M1}^$^A(!31}(",M1}^$^A(!32}(",M1}^$|
%|^A(!33}(",M1}^$^A(!34}(",M1}^$^A(!41}(",M1}^$^A(!42}(",M1}^$^|
%|A(!43}(",M1}^$^A(!44}(",M1}}} : ;8/<:";bP8ainv1: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {A}^{-1}&=\left({\begin{matrix}{A}_{11}^{-1}&{A}_{12}^{-1}&{A}_{13}^{-1}&{A}_{14}^{-1}\\
{A}_{21}^{-1}&{A}_{22}^{-1}&{A}_{23}^{-1}&{A}_{24}^{-1}\\
{A}_{31}^{-1}&{A}_{32}^{-1}&{A}_{33}^{-1}&{A}_{34}^{-1}\\
{A}_{41}^{-1}&{A}_{42}^{-1}&{A}_{43}^{-1}&{A}_{44}^{-1}\end{matrix}}\right) \label{ainv1}\end{align}
In this case, we can write an explicit expression for the quadratic invariants
${\varepsilon }_{1}$ and ${\varepsilon }_{2}$. In particular, we can look at two particular coefficients:
%
%]|Expr|[#b @`b___})4# b'4" Chicago^: ;bP8&c0!*.["!Helvetica|:! |
%|<2^1^2}<2("$^""Symbol^:"&c0  .V_^"#*|:#&c0!*2$^:"&c0  e^:#&c0!*1|
%|_}(":"&c0  .V$^:#&c0!*p^x^2}}:!,F,]$^$^:#A(!12}(",M1}_^2,K$^$|
%|^A(!22}(",M1}_^2:! :" :!-<-<:" :! <2^1^2}<2("$^:"&c0  .V_^:#&c0!*2|
%|$^:"&c0  e^:#&c0!*2_}(":"&c0  .V$^:#&c0!*p^x^2}}:!,F,]$^$^:#A|
%|(!32}(",M1}_^2,K$^$^A(!42}(",M1}_^2: .K|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{subequations}\begin{align} {1 \over 2}{{\partial }^{2}{\varepsilon }_{1} \over \partial {p}_{x}^{2}}&={{A}_{12}^{-1}}^{2}+{{A}_{22}^{-1}}^{2}  \\  {1 \over 2}{{\partial }^{2}{\varepsilon }_{2} \over \partial {p}_{x}^{2}}&={{A}_{32}^{-1}}^{2}+{{A}_{42}^{-1}}^{2}\end{align}\end{subequations}

The program  \htm{invariant_general.f90} computes invariants in 2-d-f with or without the momentum dependence $\delta =\delta p/p_0$. Also, in the case of the linear maps, it ties two (if not three) totally compatible definitions of the lattice functions in the symplectic case. For example the horizontal beta function can be viewed as the dependence $<x^2>$ on $J_1$ or as the coefficient of $p_x^2$ of the invariant ${\varepsilon }_1$. The view of lattice functions as averages is explained in \sec{seclatgenave}. The connection between matrices (deMoivre's formula), averages and invariants is  reviewed in \app{appdemoivre} for the adventurous.

{\footnotesize
\begin{verbatim}
   small misalignments and rotations in lattice ? input t or f
t
  Map with only 4d or with delta (4d+1)?
 Type t (true) for only 4d
t
  input an order > 1 (not too big first)!
2
 closed orbit =   0.19321E-04 -0.12493E-04  0.14579E-03  0.45737E-04

 etall    1, NO =    2, NV =    4, INA =   23
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     2      NV =     4
   2  0.2826538454077229       2  0  0  0
   2   1.810530523208837       1  1  0  0
   2   6.426969202080966       0  2  0  0
   2 -0.4777657262005190E-02   1  0  1  0
   2  0.5875859719868294E-01   0  1  1  0
   2 -0.5959290214920970E-01   1  0  0  1
   2 -0.4792033981819018       0  1  0  1
   2  0.4088979433177596E-03   0  0  2  0
   2 -0.2523125204238815E-02   0  0  1  1
   2  0.9033203744021414E-02   0  0  0  2
   -10   0.000000000000000       0  0  0  0

 etall    1, NO =    2, NV =    4, INA =   24
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     2      NV =     4
   2  0.4099931430271811E-03   2  0  0  0
   2  0.2625367151852487E-02   1  1  0  0
   2  0.9330049706923029E-02   0  2  0  0
   2  0.4778862387288096E-02   1  0  1  0
   2 -0.5883414173009098E-01   0  1  1  0
   2  0.5950956832569777E-01   1  0  0  1
   2  0.4791664554953957       0  1  0  1
   2  0.2819058250718235       0  0  2  0
   2  -1.739868740579244       0  0  1  1
   2   6.221539315769093       0  0  0  2
   -10   0.000000000000000       0  0  0  0

 etall    1, NO =    2, NV =    4, INA =   56
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     2      NV =     4
   1   6.426969202080966       1  0  0  0
   1  0.9330049706922264E-02   0  0  1  0
    -2   0.000000000000000       0  0  0  0
 Coefficient of invariant 1 and 2 as a function of px**2 6.4269692020809659   9.33004970692302878E-003
 Dependence of <x**2> as a function of J_1 and J_2       6.4269692020809659   9.33004970692226376E-003

  hit return to terminate program
\end{verbatim}
}



\section{ \color{.}The nonlinear invariants in one or several degrees of freedom}\label{secampcsin}

There is nothing to be added to the previous section (\sec{secampcsi}). Please run again \htm{invariant_general.f90} and input an order greater than 2.  Nothing special is needed and nothing spectacular will happen: this is the beauty of the tools based on TPSA and maps.  


\section{ \color{.} The tunes: in glorious generality (well spin excluded here)}\label{sectunegen}

This section is a rush job for my friend Piotr. I want to say that it could have included spin, but enough of an overload for the moment! 

Additionally, if the FPP package was based on a complex TPSA, this section would almost be unnecessary: learn the meaning of a vector field and leave me alone because obviously the vector field in the normalised variables give you the tunes, the damping decrements and, when the energy is constant, the dependence of time (or path length) on the various invariants. The code \htm{tunes.f90} contains several examples as explained later. It also computes the tunes/damping/compaction directly from the normalised vector field. This reproduces the answer which is stored in the so-called DHDJ array of the normal form. But additionally,
damping can be extracted. 


This section is necessary because with a ``real'' TPSA there is no easy concatenation into the phasors basis: however, imaginary TPSA coming soon for real! (Sorry for the cheap pun....)
 
With a real TPSA it is necessary for the programmer to provide a special ``resonance'' version of the vector field object. It cannot be gotten via a complex similarity transformation.  The most important fragment  of the code \htm{tunes.f90} is:

{\small
\begin{verbatim}
!!! The ray is really the result of a truncated power series algebra
!!! The map we are interested to compute is around the closed orbit: 
!!!  it is really part of a diffential algebra
m=ray   !   The ray is "officially" turned into a "damapspin"  (DA in Berz's honour)

normal = m  ! The map is normalised Orbital+Modulation

diagonal_field=normal%n%normal%nonlinear

do i=1,c_%nd2
 diagonal_field%sin%v(i)=diagonal_field%sin%v(i)/twopi
enddo
\end{verbatim}

The object  normal\%n\%normal\%nonlinear is the normalised vector field in cartesian coordinates: 
\begin{itemize}
\item  They move on  circles if all the planes are oscillators
\item they might be sink in the orbital planes if radiation is  present: circular shaped toilet bowls. The toilet bowls can be linear or amplitude dependent if one expects a limit cycle.
FPP allows you to choose. 
\item if the energy is constant, they might represent nonlinear drifts as a function of transverse circles in the $6^{th}$ plane.
\item magnet modulations only add extra circles: at present only one modulation frequency possible in FPP, i.e., one extra degree of freedom.
\end{itemize}

The object  diagonal_field is of type vecresonance. De facto, equating a real vector field such as normal\%n\%normal\%nonlinear with  diagonal_field puts it in phasors basis. So the syntax is just:

{\small
\begin{verbatim}
 ! The normalised vector field is put in phasors' basis
  diagonal_field=normal%n%normal%nonlinear
\end{verbatim}
}

Actually diagonal_field has a ``cosine'' and a ``sine' part'' from which one can reconstruct the full polynomial; this is identical to the type ``taylorresonance''  which is used in the example program as well. Thus, more precisely we have (in 1-d-f for simplicity):
%
%]|Expr|[#b @`b___})3# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|F""*|:",H$^z^1_,L$^z^2_,I:!,F,]  <c%"C^<cY A^<c%#D("$^A("$^m^|
%|1_$^m^2_}_<c!(A(%$^:"z^1^$^m^1_$^z^2^$^m^2_,K$^z^1^$^m^2_$^z^|
%|2^$^m^1_}}}(":!-<sum}^:"m_}}(#"#Monaco^:#f,Ecos}_}:",Ki<c%"C^|
%|<cY A("<c%#D("$^:!B("$^m^1_$^m^2_}_<c!(A(%$^:"z^1^$^m^1_$^z^2|
%|^$^m^2_,M$^z^1^$^m^2_$^z^2^$^m^1_}}}(":!-<sum}^:"m_}:! }}(#:#f|
%|,Esin}_}: ;8/<:";bP8vecres: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} F({z}_{1},{z}_{2})&=  \underbrace{\sum\limits_{m}^{} {A}_{{m}_{1}{m}_{2}}\left\{{{z}_{1}^{{m}_{1}}{z}_{2}^{{m}_{2}}+{z}_{1}^{{m}_{2}}{z}_{2}^{{m}_{1}}}\right\}}\limits_{{\rm f}\%{\rm c}{\rm o}{\rm s}}^{}+i\underbrace{\sum\limits_{m}^{} {B}_{{m}_{1}{m}_{2}}\left\{{{z}_{1}^{{m}_{1}}{z}_{2}^{{m}_{2}}-{z}_{1}^{{m}_{2}}{z}_{2}^{{m}_{1}}}\right\} }\limits_{{\rm f}\%{\rm s}{\rm i}{\rm n}}^{}\label{vecres}\end{align}
where the variables $z_1$ and $z_2$ are in the phasors basis, i.e.,
%
%]|Expr|[#b @`b___})5# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"z^1_,]x,Ki $^p^x_"#Monaco^:# and $^:"z^2_,]x,Mi $^p^x|
%|_:! : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {z}_{1}=
x+i\ {p}_{x}~{\rm a}{\rm n}{\rm d} ~{z}_{2}=
x-i\ {p}_{x} \end{align}
and $(x,p_x)$ move on circles or circular sinks.

\subsection{\color{.}The case of a (damped) nonlinear oscillator}

In the case of an oscillator, ideally if we had a complex TPSA, the diagonalised vector field would be of the form:
%
%]|Expr|[#b @`b___}),# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c$%!^F}""Symbol^:"&c0  /7<c$%!^/1}:!&c0!*,F,] <c%#D(%"#*|:# |
%|$^f^i_<c!$1^<c$%!^I}}$^z^i_<2(&:"&c0  .V:!&c0!*-<hphantom-[$^|
%|:#z^i_:!-]}(":"&c0  .V$^:#&c0!*z^i_}}}(":!-<sum}^:#i_}: ;8/<:#;bP8vecfr|
%|: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \vec{F}\cdot \vec{\nabla }&= \sum\limits_{i}^{} \ {f}_{i}\left({\vec{I}} \right){z}_{i}{\partial \hphantom{{z}_{i}} \over \partial {z}_{i}}\label{vecfr}\end{align}
The quantity $\vec{I}$ represents symbolically the invariants of the maps or the circular vortices of the toilet bowl if one tries to compute limit cycles\footnote{With radiation, tune/damping shift with amplitude terms are very much like the single resonance driving terms of a Hamiltonian map}. These invariants are made of equal powers of the phasor $z_1$ with its complex conjugate $z_2$. Let us be explicit on one of the simplest examples. Please run the code as follow:
{\footnotesize
\begin{verbatim}
  small misalignments and rotations in lattice ? input t or f
f
  Map with  6d without cavity -> 1
  Map with only 4d + delta  -> 2
  Map with only 6d + cavity  -> 3
  Map with only 6d + radiation + cavity  -> 4
2
  Modulate the magnet Bend1 ? yes -> t, no -> f
f
  input an order > 0 (not too big first)!
3
  PARENT LAYOUT NAME :No name assigned
  NUMBER OF ORIGINAL LAYOUT ELEMENTS :         529
  NUMBER OF THIN OBJECTS :        3269
  TOTAL IDEAL LENGTH OF STRUCTURE :   196.838520617726
  TOTAL INTEGRATION LENGTH OF STRUCTURE (mad8 style survey) :
   196.838520617726
 closed orbit =    0.0000       0.0000       0.0000       0.0000       0.0000       0.0000

\end{verbatim}
}

The results are printed in the file {\tt results.txt}.

{\footnotesize
\begin{verbatim}
 closed orbit =    0.0000       0.0000       0.0000       0.0000       0.0000       0.0000

  Spin Map is identity : not normalized

  Real Tunes (damping or momentum compaction effects )

  Should be zero: no damping and no momentum compaction effects

           1  Factorization represented

 etall    1, NO =    3, NV =    5, INA =   68
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
    -1   0.000000000000000       0  0  0  0  0

 etall    1, NO =    3, NV =    5, INA =   69
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
    -1   0.000000000000000       0  0  0  0  0

 etall    1, NO =    3, NV =    5, INA =   70
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
    -1   0.000000000000000       0  0  0  0  0

 etall    1, NO =    3, NV =    5, INA =   71
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
    -1   0.000000000000000       0  0  0  0  0

 Ordinary Tunes (or imaginary part of vector field)

           1  Factorization represented

 etall    1, NO =    3, NV =    5, INA =   72
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
   1 -0.2791031765325681       1  0  0  0  0
   2  -24.63584035902031       1  0  0  0  1
   3   13401.66207833725       2  1  0  0  0
   3   27756.70654063197       1  0  1  1  0
   3   397.5751797541163       1  0  0  0  2
    -5   0.000000000000000       0  0  0  0  0

 etall    1, NO =    3, NV =    5, INA =   73
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
   1  0.2791031765325681       0  1  0  0  0
   2   24.63584035902031       0  1  0  0  1
   3  -13401.66207833725       1  2  0  0  0
   3  -27756.70654063197       0  1  1  1  0
   3  -397.5751797541163       0  1  0  0  2
    -5   0.000000000000000       0  0  0  0  0

 etall    1, NO =    3, NV =    5, INA =   74
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
   1 -0.2523140360948593       0  0  1  0  0
   2  -25.76378518361371       0  0  1  0  1
   3   27756.70654063184       1  1  1  0  0
   3   10381.76952075711       0  0  2  1  0
   3   423.2830392559213       0  0  1  0  2
    -5   0.000000000000000       0  0  0  0  0

 etall    1, NO =    3, NV =    5, INA =   75
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
   1  0.2523140360948593       0  0  0  1  0
   2   25.76378518361371       0  0  0  1  1
   3  -27756.70654063184       1  1  0  1  0
   3  -10381.76952075711       0  0  1  2  0
   3  -423.2830392559213       0  0  0  1  2
    -5   0.000000000000000       0  0  0  0  0

 Ordinary Tunes using dhdj (works only for symplectic maps)

  In real normalised variables

 etall    1, NO =    3, NV =    5, INA =   48
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
   0  0.2791031765325681       0  0  0  0  0
   1   24.63584035902031       0  0  0  0  1
   2  -13401.66207833725       2  0  0  0  0
   2  -13401.66207833725       0  2  0  0  0
   2  -27756.70654063197       0  0  2  0  0
   2  -27756.70654063197       0  0  0  2  0
   2  -397.5751797541163       0  0  0  0  2
    -7   0.000000000000000       0  0  0  0  0

 etall    1, NO =    3, NV =    5, INA =   49
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
   0  0.2523140360948593       0  0  0  0  0
   1   25.76378518361371       0  0  0  0  1
   2  -27756.70654063184       2  0  0  0  0
   2  -27756.70654063184       0  2  0  0  0
   2  -10381.76952075711       0  0  2  0  0
   2  -10381.76952075711       0  0  0  2  0
   2  -423.2830392559213       0  0  0  0  2
    -7   0.000000000000000       0  0  0  0  0

  In phasors variables : should look like vector field results above

 etall    1, NO =    3, NV =    5, INA =   50
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
   0  0.2791031765325681       0  0  0  0  0
   1   24.63584035902031       0  0  0  0  1
   2  -13401.66207833725       1  1  0  0  0
   2  -27756.70654063197       0  0  1  1  0
   2  -397.5751797541163       0  0  0  0  2
    -5   0.000000000000000       0  0  0  0  0

 etall    1, NO =    3, NV =    5, INA =   51
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     3      NV =     5
   0  0.2523140360948593       0  0  0  0  0
   1   25.76378518361371       0  0  0  0  1
   2  -27756.70654063184       1  1  0  0  0
   2  -10381.76952075711       0  0  1  1  0
   2  -423.2830392559213       0  0  0  0  2
    -5   0.000000000000000       0  0  0  0  0
\end{verbatim}
}
The reader will notice that the vector field result agrees perfectly with the Hamiltonian result in ``DHDJ'' as described in the FPP manual included with this document.

In the case of a damped map, the normal form can be total, i.e., a nonlinear sink, or partial. In the case of a partial normalisation, the damping can depend on amplitude.
This allows the computation of limit cycles. 

Here is, for example, the  simple piece of code to get the tunes and damping from the vector field. It is part of the program  \htm{tunes.f90}.

{\footnotesize
\begin{verbatim}
write(mf,*) " "
write(mf,*) "Ordinary Tunes extracted from the normalised vector field"
if(sta==1) write(mf,*) " Momentum compaction included as the third polynomial"
write(mf,*) " "
do i=1,c_%nd
!!!  In the absence of a complex TPSA,
!!!  "convert" allows conversion between a phasor and cartesian basis 
!!!  without complex concatenation
 convert%sin=0.0_dp 
 convert%cos=tunes(i)
 tunes(i)=convert
 call print(tunes(i),mf,prec)
enddo
\end{verbatim}
}

The entity DHDJ contains also the map in cartesian coordinates: this is useful is the tunes/damping/compaction must be expressed in the original variables.
This is done by the fragment of code:


{\footnotesize
\begin{verbatim}
write(mf,*) " "
write(mf,*) "Ordinary Tunes as a function of the original phase space"
if(sta==1) write(mf,*) " Momentum compaction included as the third polynomial"
write(mf,*) " "
do i=1,c_%nd
 tunes(i)= tunes(i)*normal%n%a_t**(-1)
 call print(tunes(i),mf,prec)
enddo
\end{verbatim}
}


Please play with all the options of the code and give the author some feed back.


\subsection{\color{.}The case of nonlinear oscillator with a constant energy in the longitudinal plane}

The reader should run ``Map with  6d without cavity -> 1'', in which case the momentum compaction will pop out the real part of the vector field and will agree perfectly with the
content of ``DHDJ''. The vector field in the path length (or temporal dimension) is of the form:

%
%]|Expr|[#b @`b___})5# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"F^6_<2(&"#Symbol^:#&c0  .V:!&c0!*-<hphantom-[$^:"z^i_|
%|:!-]}(":#&c0  .V$^:"&c0!*z^6_}}:!,F,]$^:"F^6_<c!$1(#<c$%!^I},[|
%|:#&c0  d}}<2(&.V:!&c0!*-<hphantom-[$^:"z^i_:!-]}(":#&c0  .V$^|
%|:"&c0!*z^6_}}: ;8/<:";bP8vecf6: ;8/=:#;bP8 :!-<-<:# :! :#&c0  d|
%|:!&c0!*,F,]$^z^5_ : .O-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {F}_{6}{\partial \hphantom{{z}_{i}} \over \partial {z}_{6}}&={F}_{6}\left({\vec{I};\delta }\right){\partial \hphantom{{z}_{i}} \over \partial {z}_{6}}\label{vecf6} \\  \delta &={z}_{5} \nonumber 
\end{align}

The polynomial  ${F}_{6}$ contains the longitudinal variable in terms of the invariants in phasors' basis.



\section{\color{.} The spin invariant beyond linear: the vector %
%]|Expr|[#b @`b___})%# b'4" Helvetica|: ;bP8&c0!*,D<c$%!^n},H<c$%!|
%|^z},I,D}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
$\vec{n}(\vec{z})$
 }
}\label{secspinfpp}

The theory is summarized in \app{appspin}. We should immediately look at the sample code \htm{invariant_general_spin.f90}. We first compare  it with the code of \sec{seccs}, i.e.,  \htm{invariant_general.f90}.
We will then compare with stroboscopic tracking in \sec{secspinstrobo} using the code \htm{stroboscopic.f90}.

\subsection{\color{.} Normal form using the real FPP
 }\label{secspinfppr}

We notice that the codes are identical except that the object tracked and analysed are slightly different. The ray must now be of type probe_8 which includes spin:
\begin{verbatim}
  type probe_8
     type(real_8) x(6)     ! Polymorphic orbital ray
     type(spinor_8) s(ISPIN0R:ISPIN1R)   ! Polymorphic spin s(1:3)
     type(rf_phasor_8) AC  ! Modulation of magnet
     real(dp) E_ij(6,6)   !  Envelope for stochastic radiation
     !   stuff for exception
     logical u
     type(integration_node),pointer :: lost_node
  end type probe_8
\end{verbatim}
In our example, the ray and 3 three spin axes will be tracked.

On the FPP side of things, a new map, of type damapspin, containing spin is needed:
\begin{verbatim}
  type damapspin
     type(damap) M
     type(spinmatrix) s
     real(dp) e_ij(6,6) ! stochastic envelope
  end type damapspin
\end{verbatim}
One notices that the damapspin contains the orbital map. Finally this map is normalised using a object of type normal_spin:
{\footnotesize
\begin{verbatim}
  type normal_spin
     type(normalform) N   ! regular orbital normal form
     type(damapspin) a1   ! brings to fixed point
     type(damapspin) ar   ! normalises around the fixed point
     type(damapspin) as   ! pure spin map
     type(damapspin) a_t  ! !! (a_t%m,a_t%s) 
!!!  extra spin info
     integer M(NDIM,NRESO),MS(NRESO),NRES  ! orbital and spin resonances to be left in the map
     type(real_8) n0(3)     ! n0 vector
     type(real_8) theta0    !  angle for the matrix around the orbit (analogous to linear tunes)
     real(dp) nu    !  spin tune
!!!Envelope radiation stuff to normalise radiation (Sands's like theory)
     real(dp) s_ij0(6,6)  !  equilibrium beam sizes
     complex(dp) s_ijr(6,6)  !  equilibrium beam sizes in resonance basis
! equilibrium emittances (partially well defined only for infinitesimal damping)
     real(dp) emittance(3),tune(3),damping(3)   
     logical(lp) AUTO,STOCHASTIC
     real(dp)  KICK(3)   ! fake kicks for tracking stochastically
     real(dp)  STOCH(6,6)  ! Diagonalized stochastic part of map for tracking stochastically
     real(dp)  STOCH_inv(6,6)  ! Diagonalized stochastic part of map for tracking stochastically
  end type normal_spin
\end{verbatim}
}
Thus the code of \sec{seccs} is remarkably similar to the code used for the spin as far as the computation of the invariants are concerned:

\begin{verbatim}
!!! Same as the old code except that we must drag
!   the orbital part of the maps (damap out of damapspin)
!   if ID is a damapspin, then orbital part is in id%m
radii(1)=id%m%v(1)**2+id%m%v(2)**2 
radii(2)=id%m%v(3)**2+id%m%v(4)**2
!!! Same as the old code except that we must drag
!   the orbital part of the normal form (normalform out of normal_spin)
!   if normal is a normal_spin, then orbital part is in normal%n
invariant(1)=radii(1)*normal%n%A_T**(-1)
invariant(2)=radii(2)*normal%n%A_T**(-1)
\end{verbatim}

One notices that the orbital part of the damapspin and the normal form are simply substructures of the spin structures damapspin and normal_spin. This results from the fact that spin is a spectator in FPP and PTC: no Stern-Gerlach terms are present. Therefore we expect identical results between the orbital and the spin codes are far as lattice functions are concerned.

{\footnotesize \color{blue}
\begin{verbatim} 
  small misalignments and rotations in lattice ? input t or f
t
  Map with only 4d or with delta (4d+1)?
 Type t (true) for only 4d
t
  input an order > 1 (not too big first)!
2
 closed orbit =   0.19321E-04 -0.12493E-04  0.14579E-03  0.45737E-04
  PARENT LAYOUT NAME :No name assigned
  NUMBER OF ORIGINAL LAYOUT ELEMENTS :          70
  NUMBER OF THIN OBJECTS :         620
  TOTAL IDEAL LENGTH OF STRUCTURE :   90.2239999999996
  TOTAL INTEGRATION LENGTH OF STRUCTURE (mad8 style survey) :
   89.8066920140840

  Transverse Orbital Invariants

 etall    1, NO =    2, NV =    4, INA =   43
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     2      NV =     4
   2  0.2826538456124286       2  0  0  0
   2   1.810530524524741       1  1  0  0
   2   6.426969206755015       0  2  0  0
   2 -0.4777654490377422E-02   1  0  1  0
   2  0.5875858889894511E-01   0  1  1  0
   2 -0.5959289342838527E-01   1  0  0  1
   2 -0.4792032827144841       0  1  0  1
   2  0.4088977392934371E-03   0  0  2  0
   2 -0.2523123947409992E-02   0  0  1  1
   2  0.9033199240578035E-02   0  0  0  2
   -10   0.000000000000000       0  0  0  0

 etall    1, NO =    2, NV =    4, INA =   44
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     2      NV =     4
   2  0.4099929385556518E-03   2  0  0  0
   2  0.2625365841401753E-02   1  1  0  0
   2  0.9330045045003506E-02   0  2  0  0
   2  0.4778859627642768E-02   1  0  1  0
   2 -0.5883413334068530E-01   0  1  1  0
   2  0.5950955961935833E-01   1  0  0  1
   2  0.4791663397645695       0  1  0  1
   2  0.2819058252771218       0  0  2  0
   2  -1.739868741845759       0  0  1  1
   2   6.221539320274312       0  0  0  2
   -10   0.000000000000000       0  0  0  0
\end{verbatim}
}
Here,  as in \sec{seccs}, we produced the coupled quadratic invariants. The results are identical.


Next the code produces the invariant spin field using normal form. For fun, we check \eq{eqdef}. This is done by piece of code:
{\footnotesize \color{blue}
\begin{verbatim} 
n_axis=a_s*n_axis ! n=A_s(0,1,0)
 
Write(6,*) " Invariant Spin Field (ISF)"
call print(n_axis,6)

id=1;   ray=RAY_CLOSED+id;   ! ray= closed orbit + identity map

ray%s(1)=n_axis

CALL TRACK_PROBE(PSR,RAY,STATE,FIBRE1=1) ! Tracking the ISF using BMT
!!! ray%s(1) contains the transformed ISF

id=ray   
n_axis=n_axis*id%m  ! Transforming the ISF by the orbital map

n_axis%x(1)=n_axis%x(1)-ray%s(1)%x(1)
n_axis%x(2)=n_axis%x(2)-ray%s(1)%x(2)
n_axis%x(3)=n_axis%x(3)-ray%s(1)%x(3)
write(6,*) "Computing the difference between BMT on n and n o m "
call print(n_axis,6)
\end{verbatim}
}

Here PTC is used to spin directly with the BMT equation. Of course we could have use the Taylor spin one-turn map as well.
Secondly we transform the map with by the orbital part {\tt id\%m}. The two vectors are identical as they should for the invariant spin field.


\subsection{\color{.} Comparison with Stroboscopic Tracking}\label{secspinstrobo}


First\footnote{This program also allows the reading of a PTC flat file from the madgino folder. Please look at \app{chapmadptc}.} allow me the vanity and insolence to quote myself. The reader can look at this KEK technical note\cite{propa} if he is more interested at my rambling on normal forms:
\begin{quote}
The ``stroboscopic'' average is  a novel technique (see reference \cite{georgbook}, p. 142) first proposed by Heinemann and Hoffst\"atter  for the computation of the invariant spin axis $\vec n$. 
I put the word stroboscopic in quotation mark because the expression is highly unfortunate for a person dealing primarily with a map based theory. In a map based theory, a  stroboscopic average would involved recording data not at every turn but once in a while. We assume that Heinemann and Hoffst\"atter were thinking about equations of motion when they coined the term; their paper confirms the prejudice. A true stroboscopic average is rarely discussed in accelerator theory; for example dispersion can be defined rigorously in the presence of 3 tunes as a stroboscopic skipping over $p$ turns if the longitudinal tune $\nu_{3}$ is nearly equal to $q/p$ where $q$ and $p$ are integers. We will retain the term stroboscopic average by respect for the originators and also to avoid undue confusion.
\end{quote}

Once it is clear that the invariant spin axis can be gotten through a normal form, it is almost obvious that it should emerge from an average. Nevertheless, this fact was only discovered recently despite very early DESY papers on spin normal form by Yokoya. 

 The code \htm{stroboscopic.f90} performs a stroboscopic average and a simple normal form to leading order for the computation of $\vec{n}(\vec{z})$. The reader is invited to inspect it. The only difference, besides the actual stroboscopic average, is that the normal form is evaluated on a given ray in order to compare with the averaging process.
\begin{verbatim}
!!! In normal variables, the ISF is e_y=(0,1,0)
n_spinor8=2

n_spinor8=a_s*n_spinor8   ! ISF=A e_y  (see theory)

n_spinor=n_spinor8*dr !  Invariant spin field is evaluated  
\end{verbatim}

In the above code fragment, the last line is the numerical evaluation of the invariant spin field for the ray which deviates from the closed orbit by a vector {\tt dr}.

\chapter{Analysis in FPP/PTC: Normal Form for Averages}\label{chanormave}


\section{\color{.} A bit of theory to show the usefulness of normal forms}\label{secamplat}


How is it computed? At this stage, who cares!
The important point is  that you have these maps from a reliable black box library.

If you have a normal form package, you can use it the same way you use any numerical recipe package.
The first, and most important application is the computation of averages and extrema. The numerical value of these objects 
can only depend on the invariants of the trajectory if they exist. We will look at averages because they are easy to compute in the most general situation.
Extrema are only simple in the linear case.

Imagine a function $F$ (in one degree of freedom $(z_1,z_2)$ for simplicity) whose time average we desire:  
%
%]|Expr|[#b @`b___})0# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^""*|:"F}:!,F:",]<2(2F,KF"#Symbol^:#&c0  .P:"&c0!*m,KF:#&c0  .P|
%|:"&c0!*m:#&c0  .P:"&c0!*m,K'b M:! "$Times|:$,K:"F:#&c0  .P$^:"&c0!*m|
%|_^N}^N}   N:#&c0  .N.E:!&c0!* : ;8/<:";bP8fave: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{F}\right\rangle&=
{F+F\circ m+F\circ m\circ m+\cdots +F\circ {m}^{N} \over N}\ \ \ N\rightarrow \infty  \label{fave}\end{align}


Here the map $m$ is the one-turn map at the position where the average is needed. 
In the general case\footnote{By ``general case'', I really mean general: 20 degrees of freedom, sixth order, with magnet modulation and parameter dependence,  spin, etc\dots !!!}, this average is nearly impossible to carry out analytically. However, imagine that we have a normal form for $m$:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"m:!,F,]"#Times|:#a"$Symbol^:$&c0  .P:"&c0!*r:$&c0  .P$^|
%|:"&c0!*a_(",M1}:! : ;8/<:";bP8nfave: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} m&=a\circ r\circ {a}^{-1} \label{nfave}\end{align}
The map $a$ is some horrible transformation and $r$ is an amplitude dependent rotation. 
Amplitude dependent rotations, in 1-d-f, have the following form:
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"r<c!$1^["! ^$^z^1_^$^z^2_}}:!,F,] <c!$1^["" ($-<cos"#Symbol^:#&c0  q|
%|<c!$1^:"&c0!*J}}($:!-<sin:#&c0  q<c!$1(":"&c0!*J}}}(%,M:!-<sin|
%|:#&c0  q<c!$1^:"&c0!*J}}($:!-<cos:#&c0  q<c!$1^:"&c0!*J}}}}<c!$1|
%|^["! ^$^z^1_^$^z^2_}}"$Monaco^:$  where :"2J,]$^z^1^2,K$^z^2^|
%|2: ;8/<:";bP8rotave: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} r\left({\begin{matrix}{z}_{1}\\
{z}_{2}\end{matrix}}\right)&= \left({\begin{matrix}\cos\theta \left({J}\right)&\sin\theta \left({J}\right)\\
-\sin\theta \left({J}\right)&\cos\theta \left({J}\right)\end{matrix}}\right)\left({\begin{matrix}{z}_{1}\\
{z}_{2}\end{matrix}}\right)~~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~2J=
{z}_{1}^{2}+{z}_{2}^{2}\label{rotave}\end{align}
%
One notices that \eq{rotave} is not a linear map because the angle $\theta $ is a function of the amplitude $J$.





Using \eq{nfave}, we can rewrite \eq{fave}:
%
%]|Expr|[#b @`b___})b E# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^""*|:"F}:!,F:",]<2)4# b'4F"#Symbol^:#&c0  .P"$Times|:$&c0!*a|
%|:#&c0  .P$^:"&c0!*a_(",M1},KF:#&c0  .P:$&c0!*a:#&c0  .P:"&c0!*r|
%|:#&c0  .P$^:"&c0!*a_(",M1},K'b M:! :$,K:"F:#&c0  .P$)!# b'4<c!(A|
%|(%:$&c0!*a:#&c0  .P:"&c0!*r:#&c0  .P$^:"&c0!*a_(",M1}}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_^N}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|^N}   N:#&c0  .N.E:!&c0!* : ;bP;/":!;bP8,F:",]<2)2# b'4F:#&c0  .P|
%|:$&c0!*a:",KF:#&c0  .P:$&c0!*a:#&c0  .P:"&c0!*r,K'b M:! :$,K:"F|
%|:#&c0  .P:$&c0!*a:#&c0  .P$)!# b'4:"&c0!*r|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_^N}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|^N}:#&c0  .P$^:"&c0!*a_(",M1}   N:#&c0  .N.E: ;bP;&c0!*/":!;bP8 |
%|,F,]<2),# b'4<c$%A^:"F},K<c$%A^F}:#&c0  .P:"&c0!*r,K'b M:! :$,K|
%|<c$%A^:"F}:#&c0  .P$)!# b'4:"&c0!*r}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|_^N}& b!( b"0 b#8 b$@ b%H b&P!WW}^N}:#&c0  .P$^:"&c0!*a_(",M1}|
%| N:#&c0  .N.E:"&c0!* : ;8/<:";bP8faven: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{F}\right\rangle&=
{F\circ a\circ {a}^{-1}+F\circ a\circ r\circ {a}^{-1}+\cdots +F\circ {\left\{{a\circ r\circ {a}^{-1}}\right\}}^{N} \over N}\ \ \ N\rightarrow \infty  \nonumber \\
&=
{F\circ a+F\circ a\circ r+\cdots +F\circ a\circ {r}^{N} \over N}\circ {a}^{-1}\ \ \ N\rightarrow \infty \nonumber \\
 &={\overline{F}+\overline{F}\circ r+\cdots +\overline{F}\circ {r}^{N} \over N}\circ {a}^{-1}\ N\rightarrow \infty \ \label{faven}\end{align}
%
\Eq{faven} expresses a simple fact: the average of a function  $F$ is gotten from the average of the transformed function $\overline{F}=F\circ a$ under a rotation.

Because it is easy to average a function that propagates under a rotation,
let us define the phasors basis (``resonance basis''):
%
%]|Expr|[#b @`b___})b I# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!$1^["! ^$^""*|:"r^1_^$^r^2_}}:!,F,]:"c<c!$1^["! ^$^z^1_^$^|
%|z^2_}}:!,]<c!$1^["" (#:"1,O2}(#1,O2}($,Mi,O2}(#i,O2}}}<c!$1^["! |
%|^$^z^1_^$^z^2_}}: ;bP;/":!;bP8 "#Monaco^:#   and            :!,F|
%|: ;bP;/":!;bP8 <c!$1^["! ^$^:"z^1_^$^z^2_}}:!,F,]$^:"c_(",M1}|
%|<c!$1^["! ^$^r^1_^$^r^2_}}:!,]<c!$1^["" ^:"1^i^1(",Mi}}}<c!$1|
%|^["! ^$^r^1_^$^r^2_}}: ;8/<:";bP8phasorave: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left({\begin{matrix}{r}_{1}\\
{r}_{2}\end{matrix}}\right)&=c\left({\begin{matrix}{z}_{1}\\
{z}_{2}\end{matrix}}\right)=\left({\begin{matrix}1/2&1/2\\
-i/2&i/2\end{matrix}}\right)\left({\begin{matrix}{z}_{1}\\
{z}_{2}\end{matrix}}\right)\nonumber \\
 ~~~{\rm a}{\rm n}{\rm d} ~~~~~~~~~~~~&\nonumber \\
 \left({\begin{matrix}{z}_{1}\\
{z}_{2}\end{matrix}}\right)&={c}^{-1}\left({\begin{matrix}{r}_{1}\\
{r}_{2}\end{matrix}}\right)=\left({\begin{matrix}1&i\\
1&-i\end{matrix}}\right)\left({\begin{matrix}{r}_{1}\\
{r}_{2}\end{matrix}}\right)\label{phasorave}\end{align}
We then use this phasors basis to perform an average:
%
%]|Expr|[#b @`b___})># b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c$%A^""*|:"F}"#Symbol^:#&c0  .P$^"$Times|:$&c0!*r_^k:#&c0  .P|
%|:"&c0!*c:!,F,]<c%"C^<cY A(#<c$%A^:"F}:#&c0  .P:"&c0!*c}}^$^F_|
%|^r_} :#&c0  .P:"&c0!* $^c_(",M1}:#&c0  .P$^:$&c0!*r_^k:#&c0  .P|
%|:"&c0!*c:! : ;bP;/":!;bP8 ,F,]<c%"C^<cY A(#<c$%A^:"F}:#&c0  .P|
%|:"&c0!*c}}^$^F_^r_} :#&c0  .P:"&c0!* $)!# b'4<c%"C^<cY A^<c!(A|
%|(%$^c_(",M1}:#&c0  .P:$&c0!*r:#&c0  .P:"&c0!*c}}}^:#&c0  L_}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_^:$&c0!*k: ;8/<:";bP8frave: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \overline{F}\circ {r}^{k}\circ c&=\underbrace{\overline{F}\circ c}\limits_{{F}^{r}}^{}\ \circ \ {c}^{-1}\circ {r}^{k}\circ c \nonumber \\
 &=\underbrace{\overline{F}\circ c}\limits_{{F}^{r}}^{}\ \circ \ {\underbrace{\left\{{{c}^{-1}\circ r\circ c}\right\}}\limits_{\Lambda }^{}}^{k}\label{frave}\end{align}
We then expand $F^r$:
%
%]|Expr|[#b @`b___})*# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"F_^r:!,F,]<c%#D)## b'4$^:"F(!mn}^r$^z^1^m$^z^2^n|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!-<sum}^["!!__!!!! !                  }|
%|_} : ;8/<:!fravee: /=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {F}^{r}&=\sum\limits_{\begin{matrix}\\
\end{matrix}}^{} {F}_{mn}^{r}{z}_{1}^{m}{z}_{2}^{n} \label{fravee}\end{align}
and now apply the diagonalized rotation $\Lambda $ to \eq{frave}:
%
%]|Expr|[#b @`b___}),# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"F_^r"#Symbol^:#&c0  .P$^L_^:"&c0!*k:!,F,]<c%#D)&# b'4|
%|$^:"F(!mn}^r$^z^1^m$^z^2^n:!-<exp<c!$1($:"i:#&c0  q<c!$1^:"&c0!*J}|
%|<c!$1():!-<vphantom-[:#&c0  q<c!$1^:"&c0!*J}:!-]:"n,Mm}}}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!-<sum}^["!!__!!!! !                  }|
%|_} : ;8/<:!fravek: /=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {F}^{r}\circ {\Lambda }^{k}&=\sum\limits_{\begin{matrix}\\
\end{matrix}}^{} {F}_{mn}^{r}{z}_{1}^{m}{z}_{2}^{n}\exp\left({i\theta \left({J}\right)\left({\vphantom{\theta \left({J}\right)}n-m}\right)}\right) \label{fravek}\end{align}
If the angle ${\mu }_{J}$ is irrational, then the average of $F^r$ is given by the terms of equal powers in $n$ and $m$:
%
%]|Expr|[#b @`b___})*# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^$^""*|:"F_^r}:!,F,]<c%#D)## b'4$^:"F(!nn}^r$^z^1^n$^z^2|
%|^n}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!-<sum}^["!!__!!!! !                  }|
%|_} : ;8/<:!fraved: /=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{{F}^{r}}\right\rangle&=\sum\limits_{\begin{matrix}\\
\end{matrix}}^{} {F}_{nn}^{r}{z}_{1}^{n}{z}_{2}^{n} \label{fraved}\end{align}






Now let us give a simple linear example:
%
%]|Expr|[#b @`b___})4# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!$1^["! ^$^""Times|:"z^1_^$^z^2_}}"#*|:#,]<c%"C^<cY A^<c!$1|
%|^["" ^<b R^"$Symbol^:$&c0  b_}^:#&c0!*0($,M:$&c0  a:#&c0!*,O<b R|
%|^:$&c0  b_}}(#:#&c0!*1,O<b R^:$&c0  b_}}}}}(":#&c0!*,]a}_}<c!$1|
%|^["! ^:"x^p}}"%Monaco^:%    and    :#F,]$^x_^2:! : -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left({\begin{matrix}{z}_{1}\\
{z}_{2}\end{matrix}}\right)=
\underbrace{\left({\begin{matrix}\sqrt {\beta }&0\\
-\alpha /\sqrt {\beta }&1/\sqrt {\beta }\end{matrix}}\right)}\limits_{=
a}^{}\left({\begin{matrix}x\\
p\end{matrix}}\right)~~~~{\rm a}{\rm n}{\rm d} ~~~~F=
{x}^{2} \end{align}
First, we compute  $\overline{F}$,
%
%]|Expr|[#b @`b___})4# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c$%A^""*|:"F}:!,F,]:"F"#Symbol^:#&c0  .P:"&c0!*a,]$^x_^2:#&c0  .P|
%|:"&c0!*a,]$^<c!$1("<b R^:#&c0  b_}:"&c0!*x}}_^2: ;bP;/":!;bP8 |
%|,F,]:#&c0  b$^:"&c0!*x_^2:! : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \overline{F}&=F\circ a=
{x}^{2}\circ a=
{\left({\sqrt {\beta }x}\right)}^{2}\nonumber \\
 &=\beta {x}^{2} \end{align}
and then $F^r$,
%
%]|Expr|[#b @`b___})9# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"F_^r:!,F,]<c$%A^:"F}"#Symbol^:#&c0  .P:"&c0!*c,]:#&c0  b|
%|$^:"&c0!*x_^2:#&c0  .P:"&c0!*c : ;bP;/":!;bP8 ,F,]:#&c0  b$^<c!$1|
%|^<2(#$^:"&c0!*r^1_,K$^r^2_}^2}}_^2: ;bP;/":!;bP8 ,F,]:#&c0  b|
%|<c!(A^<2('$^:"&c0!*r^1^2,K$^r^2^2,K2$^r^1_$^r^2_}^4}} : -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {F}^{r}&=\overline{F}\circ c=
\beta {x}^{2}\circ c\ \nonumber \\
 &=\beta {\left({{{r}_{1}+{r}_{2} \over 2}}\right)}^{2}\nonumber \\
 &=\beta \left\{{{{r}_{1}^{2}+{r}_{2}^{2}+2{r}_{1}{r}_{2} \over 4}}\right\}\ \end{align}
and now we retain the terms of equal powers for the average:
%
%]|Expr|[#b @`b___}))# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^$^""*|:"F_^r}:!,F,]<2^"#Symbol^:#&c0  b^:"&c0!*2}$^r^1_|
%|$^r^2_ : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{{F}^{r}}\right\rangle&={\beta  \over 2}{r}_{1}{r}_{2}\ \end{align}
Now we travel back to the space of real Floquet variables using $c^{-1}$:
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^<c$%A^""*|:"F}}:!,F,]<2^"#Symbol^:#&c0  b^:"&c0!*2}$^r^|
%|1_$^r^2_:#&c0  .P$^:"&c0!*c_(",M1} : ;bP;/":!;bP8 ,F,]<2^:#&c0  b|
%|^:"&c0!*2}<c!(A(#$^z^1^2,K$^z^2^2}},]:#&c0  b:"&c0!*J: ;8/<:";bP8bj|
%|: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{\overline{F}}\right\rangle&={\beta  \over 2}{r}_{1}{r}_{2}\circ {c}^{-1}\ \nonumber \\
 &={\beta  \over 2}\left\{{{z}_{1}^{2}+{z}_{2}^{2}}\right\}=
\beta J\label{bj}\end{align}



\noindent{ \color{red} \bf Surprise! The average of  $x^2$ is the beta function times $J$! So what is this nonsense all about: as you will see in the code examples, the above algorithm holds for the most general maps of the code PTC-- linear, nonlinear, coupled, etc \dots}

Of course, the average of $F$ is gotten from \eq{bj}: 
%
%]|Expr|[#b @`b___})/# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^F},F,]""Symbol^:"&c0  b"#*|:#&c0!*J:"&c0  .P$^:#&c0!*a_|
%|(",M1},]<2^:"&c0  b^:#&c0!*2}<c%"C^<cY A^<c!$1():"&c0  g$^:#&c0!*x|
%|_^2,K:"&c0  b$^:#&c0!*p_^2,K2:"&c0  a:#&c0!*xp}}}(%"$Monaco^:$Courant|
%|,MSnyder Invariant}_}: ;8/<:!bja: /=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{F}\right\rangle&=\beta J\circ {a}^{-1}=
{\beta  \over 2}\underbrace{\left({\gamma {x}^{2}+\beta {p}^{2}+2\alpha xp}\right)}\limits_{{\rm C}{\rm o}{\rm u}{\rm r}{\rm a}{\rm n}{\rm t}-{\rm S}{\rm n}{\rm y}{\rm d} {\rm e}{\rm r}~{\rm I}{\rm n}{\rm v}{\rm a}{\rm r}{\rm i}{\rm a}{\rm n}{\rm t}}^{}\label{bja}\end{align}

\noindent{ \color{red} \bf Once more, it is no huge surprise that the effect of $a^{-1}$ is to express the average in terms of the original variables. Thus, in the linear case, $2J$ becomes the Courant-Snyder invariant.}


Any function can be averaged by the following steps:

\begin{enumerate}

\item Find the closed orbit
\item Find the map around the closed orbit
\item Transform into a normal form made of two dimensional rotations $m=a \circ r \circ a^{-1}$. The angles of this rotations are called the fractional  tunes when measured in revolutions.
\item Substitute $a$ in the function,  $f\circ a$, express $f\circ a$ in the phasors' basis and retain terms of equal powers in $z_{2i-1} z_{2i}$.

\end{enumerate}

{ \color{blue} \bf \noindent Nota Bene: phasors can be viewed as eigenfunctions rather than just a change of variables.  This is useful in analytical calculations (see \app{chapgf}).

%
%]|Expr|[#b @`b___})3# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  F(""#*|:#j.Q}_<c!$1^<c$%!^&c0!*z}}:!,F:#,]$|
%|^z($2j,M1}_&c0  .Qi$^&c0!*z("2j}_ ,] <b R("2$^J^j_}_}$^e_)%# b'4|
%|:!-<mp i$^:"&c0  f^:#&c0!*j_}& b!( b"0 b#8 b$@ b%H b&P!WW}: ;8/<|
%|:#;bP8phasors: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\Phi }_{j\pm }\left({\vec{z}}\right)&=
{z}_{2j-1}\pm i{z}_{2j}\ =
\ \sqrt {2{J}_{j}}{e}^{\mp i{\phi }_{j}}\label{phasors}\end{align}
It is easy to check  that
%
%]|Expr|[#b @`b___})-# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  F(""#*|:#j.Q}_:".P:#&c0!*r:!,F:#,]$^e_)%# b'4|
%|:!-<mp i$^:"&c0  m^:#&c0!*j_}& b!( b"0 b#8 b$@ b%H b&P!WW}$^:"&c0  F|
%|(":#j.Q}_: ;8&c0!*/<:#;bP8eigenphasors: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\Phi }_{j\pm }\circ r&=
{e}^{\mp i{\mu }_{j}}{\Phi }_{j\pm }\label{eigenphasors}\end{align}
Thus the phasors ${\Phi }_{j \pm }$ are eigenfunctions of $r$. It must be pointed out that \eq{eigenphasors} still holds  if the rotation  $r$ is nonlinear. The tunes ${\mu }_{j}$ are functions of the action variables--- the radii. 
}


\section{\color{.} A bit of FPP implementation of the theory}\label{secamplai}

The adventurous reader can look into the subroutine AVERAGE which can be found in file o\_tree\_element.f90.
Unfortunately it lacks the transparency that a complex TPSA would offer (See below). 

Nevertheless, I will explain here the most important step in FPP:
{\footnotesize
\begin{verbatim}
    TYPE(taylorresonance) fq
        .
  fq=F*A   <---  put in Cartesian Floquet variables 
  fq%sin=0.0_dp   and then shoved into a Taylor resonance.

\end{verbatim}
}
F is the function to be averaged as in \eq{fave}, fq is of type taylorresonance. So first, the function is put in Cartesian Floquet variables using the concatenation $F\circ A$, i.e., F*A in the Fortran code (as in \eq{faven}).

Then it is shoved into a Taylor resonance fq as in \eq{frave}.  The polynomial fq is made of phasors.  Then one scans through the polynomial fq\%cos which contains the ``real'' part of the polynomial fq to compute the average. This is equivalent to \eq{fravee}, but due to a lack of a true complex Taylor TPSA, it is not done using a concatenation.

As I said, the adventurous reader can also look at the equivalent AVERAGE function of my 1-d-f demo package  at \htmt{y_my_own_analysis.f90}  {\it which uses concatenation}. Here is the relevant part of the routine:
{\footnotesize
\begin{verbatim}
!1) CREATES THE CHANGE OF BASIS FROM A ROTATION TO A FULLY DIAGONAL COMPLEX MATRIX
CALL CREATE_PHASORS(USE_J)      
 !2) APPLIES A TO THE FUNCTION AND THEN GOES INTO THE COMPLEX PHASORS BASIS
F_FLOQUET=(F*A)*TO_PHASOR**(-1)   
                                                    
DO I=0,N_MONO
  IF(JORDER(I)>MY_ORDER) CYCLE
  IF(JEXP1(I)/=JEXP2(I)) THEN ! 3) EXTRACT TERMS OF EQUAL POWERS
   F_FLOQUET%A(I)=0.0_DP
  ENDIF       
ENDDO
\end{verbatim}
}

\section{\color{.} 
%
%]|Expr|[#b @`b___})## b'4" Helvetica|: ;bP8&c0!*,D<c!,Q^$^"!*|:!z|
%|^i_}: ,D}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
$\left\langle{{z}_{i}}\right\rangle$: various dispersions } 


\subsection{\color{.} Amplitude dependent dispersions: general }  \label{secdispgen}

Averages are always computed the same way in FPP as explained in \sec{secamplat} and the code is identical to that of \sec{seclatgen} for the lattice functions.  Dispersions can be viewed as the components  of averages that are dependent on parameters only. 

The example program \htm{average_linear_general.f90} uses two lattices: a simple lattice which is also simulated with  the toy package using the file \htmt{z_my_nonlinear_twiss_average_x.f90}
and the Los Alamos PSR . The toy lattice is produced by PTC as follows:
%
\begin{verbatim}
madlength = .false.
exact_model = .false.
madkind2 = drift_kick_drift

ang = (twopi / 20.d0)
Larc = 1.d0
brho = 1.2d0 * (Larc / ang)
h=  twopi/20/Larc
call set_mad(brho = brho, method = 2, step = 100)

kf = 1.d0
kd = -1.d0

L=DRIFT("L",Larc)
qf = sbend("QF", Larc, ang).q.kf  ! <-- PUTS QUADRUPOLE KF
qd = sbend("QD", Larc, ang).q.kD  ! <-- PUTS QUADRUPOLE KD

sf = sextupole("SF", 0.d0, 1.d0) 


cell = QF+SF+L+QD+SF+L

PSR = 10 * cell
PSR = .ring.PSR
\end{verbatim}
It is a lattice for which it is ``easy'' to produce an exact formula for the dependence of $<x>$ as a function of $J$ provided
\begin{enumerate}
\item the Hamiltonian uses the small angle approximation (exact=false in PTC\footnote{The PTC example and the toy package code use a different definition of the sextupole strength., i.e.,  $b_3^{ptc}=2 b_2^{toy}$}),
\item the only nonlinearities are sextupoles kicks,
\item there are no misalignments and no error tilts.
\end{enumerate}

The conditions are too strong but they will insure agreement.  In that case, the analytical formula is given by:
%
%]|Expr|[#b @`b___})+# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<2("""Symbol^:"&c0  .V$^<c!,Q^"#*|:#&c0!*x}^s_}(":"&c0  .V:#&c0!*J}}|
%|:!,F,]<2^$^:"&c0  b^:#&c0!*s(#1,O2}^2}<c"$#)%# b'4<2((,M:!-<sin|
%|<c!$1^$^:"&c0  m(%:#&c0!*s s,K:"&c0  s}_}:#&c0!*,K:!-<sin<c!$1|
%|(#$^:"&c0  m(%:#&c0!*s s,K:"&c0  s}_:#&c0!*,M:"&c0  m}}}(%:#&c0!*1|
%|,M:!-<cos<c!$1^:"&c0  m}}}$^b^s(#:#&c0!*3,O2}$^k^:"&c0  s_:#&c0!*d|
%|:"&c0  s}& b!( b"0 b#8 b$@ b%H b&P!WW}^:#&c0!*s(#s,KC}}:! : ;8/<|
%|:#;bP8xana: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\partial {\left\langle{x}\right\rangle}_{s} \over \partial J}&={{\beta }_{s}^{1/2} \over 2}\oint_{s}^{s+C}{-\sin\left({{\mu }_{s\ s+\sigma }}\right)+\sin\left({{\mu }_{s\ s+\sigma }-\mu }\right) \over 1-\cos\left({\mu }\right)}{\beta }_{\sigma }^{3/2}{k}_{\sigma }d\sigma  \label{xana}\end{align}
({\it \color{red} The insanely adventurous reader may want to look at \app{chapgf} where I compare the Green's methods (which matches FPP) and the Fourier mode analysis (Guignard, S.Y. Lee. etc...) which requires great pain to match an FPP calculation.})

Let us run the code \htm{average_linear_general.f90} for the toy lattice:
{\color{mygreen}\small 
\begin{verbatim}
  small misalignments and rotations in lattice ? input t or f
 false
  For toy Lattice  input t or PSR input f
 true
  Give  i for <x_i>
       1
  with order=1, this code will produce only the dispersion dclosed_orbit/dx_5

  in general you get  dclosed_orbit^n/(dx_5^n1 dJ_1^n2 dJ_2^n3) (n1+2*n2+2*n3 = order)

  input an order  ---->  order > 1  if you want comparison with analytical for <x>

  2
  Since you selected an order greater than 1,
  You may want to study the code z_my_nonlinear_twiss_average_x.f90
  in build_my_own_tiny_package\my_main_programs

 closed orbit =    0.0000       0.0000       0.0000       0.0000
  average_floquet from output of AVERAGE

 etall    1, NO =    2, NV =    5, INA =   60
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     2      NV =     5
   1  -12.46383082513157       1  0  0  0  0    <-- d<x>/dJ_x
   1   8.433120636402959       0  0  1  0  0     <-- d<x>/dJ_y
   1  0.8903216225021952       0  0  0  0  1    <--- usual dispersion
   2  -1.201431435319005       0  0  0  0  2    <--- usual second order dispersion
    -4   0.000000000000000       0  0  0  0  0
                                 .
                                 .    <---- Not so important results skipped
                                 . 
  Since you have selected the toy lattice and , <x>,
  We will compare with an analytical calculation for d<x>/dJ_x
   without errors the agreement with TPSA/FPP should be perfect
   Doing a Twiss to integrate the formula for d<x>/dJ_x
  read the chapter on this topic if you are lost

 The agreement should be perfect
  Analytical result  -12.463830825131403
  FPP result         -12.463830825131573
\end{verbatim}
}
The actual code is not different from the code of \sec{seclatgen}. Analytical formulas are vastly different should we compute them.


\subsection{\color{.} Linear and Nonlinear dispersions: quick and dirty }\label{secdispdirty}

In \sec{secdispgen} the dispersion are defined as the average of a position or momentum. This is the most general approach. It permits the computation of amplitude dependent displacement of a beam.

However, most of the time, we are interested in simple changes of the close orbit as a function of a parameter. The standard parameter is the constant momentum (or energy) variable $\delta p \over p_0$.

In the example \htm{average_linear_dirty.f90}, the dispersions corresponding to $(x,p_x,y,p_y)$, are computed to third order in the fifth variable of PTC $\delta p \over p_0$.
The code is very simple; it suffices to extract the corresponding powers of the canonical transformation which diagonalises the map. 

{\footnotesize
\begin{verbatim}
 !  Here starts a universal Twiss loop
 f=>psr%start               

 ray=normal%a_t+closed_orbit   ! The ray is initialize as the map A_cs+closed_orbit


  a_t=ray

 do j=1,no1
  je=0
  je(5)=j
  do k=1,4
   disp(k,j)=a_t%v(k).sub.je  ! All the dispersions up to delta^3 are stored in disp(1:4,1:3)
  enddo
enddo
  write(mf,*) " At the beginning of the lattice " 
  write(mf,'(a19,3(1x,D18.11))') " x  dispersions -> ",disp(1,:)
  write(mf,'(a19,3(1x,D18.11))') " x' dispersions -> ",disp(2,:)
  write(mf,'(a19,3(1x,D18.11))') " y  dispersions -> ",disp(3,:)
  write(mf,'(a19,3(1x,D18.11))') " y' dispersions -> ",disp(4,:)

 
 do i=1,psr%n

  call track(psr,ray,i,i+1,state)

  a_t=ray
 
 do j=1,no1
  je=0
  je(5)=j
  do k=1,4
   disp(k,j)=a_t%v(k).sub.je  ! All the dispersions up to delta^3 are stored in disp(1:4,1:3)
 enddo
 enddo

  write(mf,*) " After Element ",f%mag%name
  write(mf,'(a19,3(1x,D18.11))') " x  dispersions -> ",disp(1,:)
  write(mf,'(a19,3(1x,D18.11))') " x' dispersions -> ",disp(2,:)
  write(mf,'(a19,3(1x,D18.11))') " y  dispersions -> ",disp(3,:)
  write(mf,'(a19,3(1x,D18.11))') " y' dispersions -> ",disp(4,:)

 f=>f%next
 enddo
\end{verbatim}
}

The above code is a simple ``Twiss loop'' where the canonical transformation is tracked--- as usual. To get the dispersion is trivially extracted as the paramete

\section{\color{.} Time or path length average:
$\left\langle{{z}_{6}}\right\rangle$ (PTC's longitudinal variable)
 } 

This section shows the computation of the linear and nonlinear time slip (momentum compaction terms). It is done first, in \sec{secmomcomp}, by averaging the time coming from a four dimensional phase space which is energy dependent. Then, it is done by a Jordan normal form--- a full six dimensional normalisation in \sec{sec3djord}. 
Both calculations are done in the program  \htm{longitudinal_no_cavity.f90}.

\subsection{\color{.} Time average: Momemtum Compaction linear and nonlinear } \label{secmomcomp}

If a particle goes around and around in a circular accelerator, in the absence of RF focussing, it makes sense to look at the transverse average of 
the time or the path length. 

This average time usually makes sense even if a  focussing agent is present like an RF cavity. This is because the resulting tune is often very small and thus the longitudinal motion is essentially adiabatic.  Indeed the well-know formula for the synchrotron tune involves only the so-called momentum compaction.  In the presence of  dispersion at  the cavities, the formula remains valid provided that the resulting tune is small-- say a few  percent or less. 

We can see how this can be true by looking at a single RF-kick around the some machine:

%
%]|Expr|[#b @`b___})7# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  t_^"#*|:#&c0!*1:!,F,]$^:"&c0  t_^:#&c0!*0,K|
%|f<c!$1(#<c$%!^r},[$^p^:"&c0  t_}}: ;bP;&c0!*/":!;bP8 $^:#p^:"&c0  t|
%|^:#&c0!*1:!,F,]$^:#p^:"&c0  t^:#&c0!*0,K<2(!qV}("$^p^0_c}}:!-<|
%|sin<c!$1)$# b'4:"&c0  w$^t_^:#&c0!*1,K:"&c0  j|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}: ;8&c0!*/<:#;bP8cavmap: ;8/=:!;bP8 |
%|: -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\tau }^{1}&={\tau }^{0}+f\left({\vec{r};{p}_{\tau }}\right)\nonumber \\
 {p}_{\tau }^{1}&={p}_{\tau }^{0}+{qV \over {p}_{0}c}\sin\left({\omega {\tau }^{1}+\varphi }\right)\label{cavmap} \end{align}

If the focussing power ${qV \over {p}_{0}c}$ is small, then the synchrotron tune is small. We can expect the energy ${p}_{\tau }$ to barely change during several transverse oscillations. 
Thus $\eq{cavmap}$ can be replaced by:
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  t_^"#*|:#&c0!*1:!,F,]$^:"&c0  t_^:#&c0!*0,K|
%|$^<c!,Q("f<c!$1(#<c$%!^r},[$^p^:"&c0  t_}}}}^<c$%!^:#&c0!*r}_|
%|: ;bP;/":!;bP8 $^:#p^:"&c0  t^:#&c0!*1:!,F,]$^:#p^:"&c0  t^:#&c0!*0|
%|,K<2(!qV}("$^p^0_c}}:!-<sin<c!$1)$# b'4:"&c0  w$^t_^:#&c0!*1,K|
%|:"&c0  j}& b!( b"0 b#8 b$@ b%H b&P!WW}}: ;8&c0!*/<:#;bP8cavmapa|
%|: ;8/=:!;bP8 : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\tau }^{1}&={\tau }^{0}+{\left\langle{f\left({\vec{r};{p}_{\tau }}\right)}\right\rangle}_{\vec{r}}\nonumber \\
 {p}_{\tau }^{1}&={p}_{\tau }^{0}+{qV \over {p}_{0}c}\sin\left({\omega {\tau }^{1}+\varphi }\right)\label{cavmapa} \end{align}

The quantity 
%
%]|Expr|[#b @`b___})## b'4" Helvetica|: ;bP8&c0!*,D$^<c!,Q(""!*|:!f|
%|<c!$1(#<c$%!^r},[$^p^""Symbol^:"&c0  t_}}}}^<c$%!^:!&c0!*r}_: ,D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
${\left\langle{f\left({\vec{r};{p}_{\tau }}\right)}\right\rangle}_{\vec{r}}$, which is a transverse phase average of the frozen (or coasting) beam,
cannot depend on the location ``$s$''. Not surprisingly, we will see later that it is part of the kernel of a normal form of its own--- a Jordan normal form.
In any event, we can compute this average, and we expect the results to be of the form:
%
%]|Expr|[#b @`b___})8# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^<c!,Q("""*|:"f<c!$1(#<c$%!^r},[$^p^"#Symbol^:#&c0  t_}}}}^<c$%!|
%|^:"&c0!*r}_:!,F,]F<c!$1)%# b'4$^:"J^x_,L$^J^y_,[$^p^:#&c0  t^|
%|:"&c0!*0}& b!( b"0 b#8 b$@ b%H b&P!WW}},]$^:!f^0_,K:#&c0  a$^|
%|:"&c0!*p^:#&c0  t^:"&c0!*0,M$^:#&c0  m^:"&c0!*x^:#&c0  .B$^:"&c0!*J|
%|^x_,M$^:#&c0  m^:"&c0!*y^:#&c0  .B$^:"&c0!*J^y_,K'b M:! : ;8/<|
%|:";bP8momcexp: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\left\langle{f\left({\vec{r};{p}_{\tau }}\right)}\right\rangle}_{\vec{r}}&=F\left({{J}_{x},{J}_{y};{p}_{\tau }^{0}}\right)=
{f}_{0}+\alpha {p}_{\tau }^{0}-{\mu }_{x}^{\prime }{J}_{x}-{\mu }_{y}^{\prime }{J}_{y}+\cdots \label{momcexp}\end{align}
In \eq{momcexp} we have anticipated a simple result of the full Jordan normal form namely the connection between the chromaticities and the amplitude dependent time or path length.


In PTC we can compute the average of \eq{momcexp} by using the state 

\begin{verbatim}
 state=delta0+time0 
\end{verbatim}

The example program is at \htm{longitudinal_no_cavity.f90}.  The average is done with a call to the FPP subroutine {\tt AVERAGE}:

{ \small
\begin{verbatim}
closed_orbit=0.d0 ! initial guess for closed orbit

call find_orbit(psr,closed_orbit,1,state,1.d-5)

write(6,'(a16,4(1x,g12.5))') " closed orbit = ",closed_orbit(1:4)

id=1;   ray=closed_orbit+id;   ! ray= closed orbit + identity map

call track(psr,ray,1,state) ! One turn map is computed via the ray

!!! The ray is really the result of a truncated power series algebra
!!! The map we are interested to compute is around the closed orbit: 
!!  it is really part of a diffential algebra
m=ray   !   The ray is "officially" turned into a "damap"  (DA in Berz's honour)

normal = m  ! The map is normalised   



!!! This averages an arbitrary function over the orbit
!!! Internally this routine suffers in clarity from the absence of a Complex TPSA  
!!! Input = function ray(6)%t and transformation 
!!! normal%A_T   -> Normal form = normal%A_T^-1 o Map o normal%A_T
call AVERAGE(ray(6)%t,normal%A_T,average_floquet,average_xp,use_J=.false.)    


!!! Output = average_floquet -> in phasors
Write(6,*) " average_floquet from output of AVERAGE"
call print(average_floquet,6)
\end{verbatim}
}

In the line where the average is performed, the quantity being averaged is ray(6)\%t which is the Taylor part of the polymorph ray(6).
Remember that PTC tracks polymorphs, not Taylor series.

The result is:

{ \small
\begin{verbatim}
  Misalignments are turned on
  input an order > 0 (not too big first)!
4
  number of degrees of freedom            2

 closed orbit =   0.19321E-04 -0.12493E-04  0.14579E-03  0.45737E-04
  average_floquet from output of AVERAGE

 etall    1, NO =    4, NV =    5, INA =   53
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     4      NV =     5
   0  0.1104515511980964E-01   0  0  0  0  0
   1  -10.66429142102492       0  0  0  0  1
   2   3.493054516585591       1  1  0  0  0
   2   7.887945813747676       0  0  1  1  0
   2   73.87265451833204       0  0  0  0  2
   3   20.38000856936365       1  1  0  0  1
   3  -56.28688213186869       0  0  1  1  1
   3  -131.3721965759499       0  0  0  0  3
   4  -3.616412231665544       2  2  0  0  0
   4   21.18554685132174       1  1  1  1  0
   4  -2.972682995185236       0  0  2  2  0
   4   112518.8189993683       1  1  0  0  2
   4  -112414.3613973485       0  0  1  1  2
   4   241.2474790638223       0  0  0  0  4
   -14   0.000000000000000       0  0  0  0  0
\end{verbatim}
}



The constant term represents a residual constant time lag on the closed orbit. The most important term is $\alpha = -10.66$. 
This term, with the voltage, controls the synchrotron tune provided that the tune remains small.


The other terms are nonlinear corrections. For example, the term ${\alpha }_2 = 73.87 $ is really the second order correction to $\alpha $ or second order momentum
compaction. 

In the next section, we will see that this average is actually embedded in a normal form--- a Jordan normal form to be more precise. It will trivially follow that the
amplitude dependent parts of the average time, namely the terms the monomials  $3.49 (x^2+p_x^2)$ and $7.79 (y^2+p_y^2)$ are proportional to the $x$ and $y$ chromaticity respectively.


\subsection{\color{.} 3-degrees-of-freedom Jordan Normal Form: Chromaticities!}\label{sec3djord}


In FPP it is possible to diagonalise the full 6-d map in the {\it absence} of a cavity. In the case, the normal form equivalent to \eq{nfave} is a Jordan normal form. It is made of two transverse planes undergoing rotation and a ``longitudinal'' (or temporal)  plane ``drifting'' along. In the normal coordinates, the time increments at each turn  by a constant amount that is a function of the energy and the transverse actions. Thus the Lie operator or pseudo-Hamiltonian for this map $r$ is of the form
%
%]|Expr|[#b @`b___})?# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"K:!,F,] "#Times|:#K,H$^J^x_,L$^J^y_,L$^:"p^"$Symbol^:$&c0  t|
%|_:#&c0!*,I"%Monaco^:% where  $^:"p^:$&c0  t_:"&c0!*,]:$&c0  d|
%|:"&c0!*,]<2^<c!$1(#p,M$^p^0_}}("$^p^0_c}}:% or <2^<c!$1(#:"E,M|
%|$^E^0_}}("$^p^0_c}}:! : ;8/<:";bP8nljordan: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} K&= K({J}_{x},{J}_{y},{p}_{\tau })~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~~{p}_{\tau }=
\delta =
{\left({p-{p}_{0}}\right) \over {p}_{0}c}~{\rm o}{\rm r}~{\left({E-{E}_{0}}\right) \over {p}_{0}c} \label{nljordan}\end{align}


From this, we get the usual phase advance $\mu $ as
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  m^"#*|:#&c0!*i_:!,F,],M<2(":"&c0  .V:#&c0!*K}|
%|(":"&c0  .V$^:#&c0!*J^i_}}  i,]x,Ly:! : ;8/<:#;bP8nlt: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\mu }_{i}&=-{\partial K \over \partial {J}_{i}}\ \ i=
x,y \label{nlt}\end{align}
and the time like variable 
%
%]|Expr|[#b @`b___})*# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""Symbol^:"&c0  Dt:!&c0!*,F,]<2(":"&c0  .V"#*|:#&c0!*K}(":"&c0  .V|
%|d}}:#&c0!* : ;8/<:#;bP8nlmomc: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \Delta \tau &={\partial K \over \partial \delta }\ \label{nlmomc}\end{align}
It is clear from \eqs{nlt} and \eqe{nlmomc} that the linear dependence of the tunes on the energy (chromaticities) and the linear dependence of the path length on the actions, are proportional to each other. This is a direct consequence of the Jordan normal form. One can also show that \eq{nlmomc} is the average time increase per turn; thus it is \eq{momcexp}.

In the code PTC, the state nocavity0 insures the computation of a 6d phase space in the presence of Taylor series. Thus we can simply normalised the resulting map and print the ``third'' tune, which is not a tune, but the derivative of the normalised pseudo-Hamiltonian by $p_{\tau }$. The code is just
{ \small
\begin{verbatim}

id=1;   ray=closed_orbit+id;   ! ray= closed orbit + identity map

call track(psr,ray,1,state) ! One turn map is computed via the ray

m=ray   !    

normal = m  ! The map is normalised   

call print(normal%dhdj%v(6),6)

\end{verbatim}
}
It is identical to the previous piece of code except that we do not need to performa an average. It is done by the normal form automatically. 


{ \small
\begin{verbatim}
  Calculation with a 6-d Jordan Normal Form

  Hit Return to continue


  number of degrees of freedom            3


 etall    1, NO =    4, NV =    6, INA =   70
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     4      NV =     6
   1  -10.66429142102492       0  0  0  0  1  0
   2   3.493054516585612       1  1  0  0  0  0
   2   7.887945813747610       0  0  1  1  0  0
   2   73.87265451833204       0  0  0  0  2  0
   3   20.38000856935378       1  1  0  0  1  0
   3  -56.28688213186190       0  0  1  1  1  0
   3  -131.3721965759486       0  0  0  0  3  0
   4  -3.616412231680643       2  2  0  0  0  0
   4   21.18554685134264       1  1  1  1  0  0
   4  -2.972682995180767       0  0  2  2  0  0
   4   112518.8189993531       1  1  0  0  2  0
   4  -112414.3613973434       0  0  1  1  2  0
   4   241.2474790708437       0  0  0  0  4  0
   -13   0.000000000000000       0  0  0  0  0  0

\end{verbatim}
}

The result is identical to that of the previous section as we advertised.


\section{%
%]|Expr|[#b @`b___})## b'4" Helvetica|: ;bP8&c0!*,D<c!,Q("$^"!*|:!z|
%|^i_$^z^j_}}: ,D}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
$\left\langle{{z}_{i}{z}_{j}}\right\rangle$ : Standard Lattice Functions } \label{seclatgenave}



\subsection{\color{.} General  computation: amplitude dependent lattice functions } \label{seclatgen}

{\color{red} \bf The sample program can be found at  \htm{average_quadratic_general.f90}. Please look!}




\noindent {\bf \color{red} Hopefully in a new FPP based on a complex things will be as in \sec{secamplat}. }

Let us run a case that displays it generality:

{\color{mygreen}
\begin{verbatim}
  small misalignments and rotations in lattice ? input t or f
t
  input an order > 1 (not too big first)!
4
 closed orbit =   0.19321E-04 -0.12493E-04  0.14579E-03  0.45737E-04
  Give  i,j for <x_i x_j>
1 1
  average_floquet from output of AVERAGE

 etall    1, NO =    4, NV =    4, INA =   54
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     4      NV =     4
   1   6.426969202080955       1  0  0  0
   1  0.9330049706930202E-02   0  0  1  0
   2   13.50076603987032       2  0  0  0
   2  -41.09211007997915       1  0  1  0
   2   12.99398516861626       0  0  2  0
    -5   0.000000000000000       0  0  0  0
                       .
                       .
                       .
\end{verbatim}                       .
}
\vskip -1.0cm
%
%]|Expr|[#b @`b___})b H# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^$^""*|:"x_^2}:!,F,]6,N43$^J^1_,K9,N33 $(!10}_(",M3}$^J^|
%|2_,K13,N50 $^J^1^2,K13,N00 $^J^2^2,M41,N09 $^J^1_$^J^2_: ;8/<|
%|:";bP8x2pn: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{{x}^{2}}\right\rangle&=6.43{J}_{1}+9.33 {10}^{-3}{J}_{2}+13.50 {J}_{1}^{2}+13.00 {J}_{2}^{2}-41.09 {J}_{1}{J}_{2}\label{x2pn}\end{align}
The first term, the dependence of $<x^2>$ on $J_1$ is the beta function. The second term is the coupled beta function as defined by Ripken.
We will see that these functions enter in the invariants as well. 

The other terms are nonlinear ``beta'' functions.

\vskips

\noindent {{\bf Remark:}  Change the following line in the main program
{\color{mygreen}
\begin{verbatim}
!!!  Phase space of Taylor maps will be (x,px,y,py)
state=only_4d0
\end{verbatim} 
{\color{red}to the following}
\begin{verbatim}
!!!  Phase space of Taylor maps will be (x,px,y,py)
state=delta0
\end{verbatim} 
}
\noindent then the program will also compute the dependence of the lattice function on delta! 

\subsection{\color{.}Lattice Function: quick and dirty } 

The TPSA package is already slow. The function AVERAGE of FPP, while general, is slow. 

Thus the normal routines Twiss loop for lattice functions will assume the following things:

\begin{enumerate}
\item\label{lat1} We are interested in linear lattice functions 
\item\label{lat2} The system  could be coupled
\item\label{lat3} it could have radiation
\item\label{lat4} it could have parameter dependence
\item\label{lat5} it be related to spin as well
\end{enumerate}
%
In this section we will look only at the situation described by cases \ref{lat1} and \ref{lat2}. Without any particular massaging, these lattice functions are the Ripken lattice functions.
If you run the general loop of the example program of \sec{seclatgen} with 
{\color{darkgreen}
\begin{verbatim}
 input an order > 1 (not too big first)! 
  2
\end{verbatim}
}
you will obtain the 2-d-f Ripken lattice function.

\noindent For example, run the example program as below:
{\color{darkgreen}
\begin{verbatim}
  small misalignments and rotations in lattice ? input t or f
t
  input an order > 1 (not too big first)!
2
 closed orbit =   0.19321E-04 -0.12493E-04  0.14579E-03  0.45737E-04
  Give  i,j for <x_i x_j>
1 1
  average_floquet from output of AVERAGE

 etall    1, NO =    2, NV =    4, INA =   52
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     2      NV =     4
   1   6.426969202080955       1  0  0  0
   1  0.9330049706930202E-02   0  0  1  0
    -2   0.000000000000000       0  0  0  0
\end{verbatim}
}
%%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^$^""*|:"x_^2}:!,F,]6,N427$^J^1_,K9,N330 $(!10}_(",M3}$^|
%|J^2_ : ;8/<:";bP8x2p: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{{x}^{2}}\right\rangle&=6.427{J}_{1}+9.330 {10}^{-3}{J}_{2} \label{x2p}\end{align}
%
%
\Eq{x2p} is the output of the program. $J_1$ and $J_2$ are the invariants to the order of the computation. Thus in this example, they are $1/2$ the coupled Courant-Snyder invariants.

{\color{red} \bf Because the map $A$ linear, we might hope to be able to compute the averages analytically in terms of the canonical transformation $A$. Indeed we can: these are the Ripken lattice functions.}
For example, the lattice function computed in \eq{x2p} is given by:
%
%]|Expr|[#b @`b___})0# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^$^""*|:"x_^2}:!,F,]<c%"C^<cY A^<c!$1(#$^A(!11}^2,K$^A(!|
%|12}^2}}}^$^"#Symbol^:#&c0  b^:"&c0!*x__}$^:!J^1_,K<c%"C^<cY A|
%|^<c!$1(#$^A(!13}^2,K$^A(!13}^2}}}^$^:#&c0  b(!:"&c0!*xy}__}$^|
%|:!J^2_ : ;8/<:";bP8x2pa: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{{x}^{2}}\right\rangle&=\underbrace{\left({{A}_{11}^{2}+{A}_{12}^{2}}\right)}\limits_{{\beta }_{x}}^{}{J}_{1}+\underbrace{\left({{A}_{13}^{2}+{A}_{13}^{2}}\right)}\limits_{{\beta }_{xy}}^{}{J}_{2} \label{x2pa}\end{align}
The general result is  simply
%
%]|Expr|[#b @`b___})/# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q("$^""*|:"z^i_$^z^j_}}:!,F,]<c%"C^<cY A^<c!$1(&$^A("i1}_|
%|A$(!}("j1}_,K$^A("i2}_$^A("j2}_}}})%# b'4"#Symbol^:#&c0  .V<c!,Q|
%|("$^:"&c0!*z^i_$^z^j_}},O:#&c0  .V$^:"&c0!*J^1_|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_}$^:!J^1_,K<c%"C^<cY A^<c!$1(&|
%|$^A("i3}_A$_("j3}_,K$^A("i3}_$^A("j3}_}}})%# b'4:#&c0  .V<c!,Q|
%|("$^:"&c0!*z^i_$^z^j_}},O:#&c0  .V$^:"&c0!*J^2_|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_}$^:!J^2_ : ;8/<:!zizj;bP8pa: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{{z}_{i}{z}_{j}}\right\rangle&=\underbrace{\left({{A}_{i1}A{}_{j1}+{A}_{i2}{A}_{j2}}\right)}\limits_{\partial \left\langle{{z}_{i}{z}_{j}}\right\rangle/\partial {J}_{1}}^{}{J}_{1}+\underbrace{\left({{A}_{i3}A{}_{j3}+{A}_{i3}{A}_{j3}}\right)}\limits_{\partial \left\langle{{z}_{i}{z}_{j}}\right\rangle/\partial {J}_{2}}^{}{J}_{2} \label{zizjpa}\end{align}
and it is computed  in the program  \htm{average_quadratic_dirty.f90}. I display the results:
{\color{mygreen}
\begin{verbatim}
  small misalignments and rotations in lattice ? input t or f
    t
  order =            1
 closed orbit =   0.19321E-04 -0.12493E-04  0.14579E-03  0.45737E-04

  <z_i*z_j> = (A_i_1*A_j_1+A_i_2*A_j_2)*J_1+(A_i_3*A_j_3+A_i_4*A_j_4)*J_2+...

  <z_1 z_1> =   6.4269692068     J_1 +  0.93300450450E-02 J_2
  <z_1 z_2> = -0.90526526226     J_1 + -0.13126829207E-02 J_2
  <z_1 z_3> = -0.23960164136     J_1 +  0.23958316988     J_2
  <z_1 z_4> = -0.29379294449E-01 J_1 +  0.29417066670E-01 J_2
  <z_2 z_2> =  0.28265384561     J_1 +  0.40999293856E-03 J_2
  <z_2 z_3> =  0.29796446714E-01 J_1 + -0.29754779810E-01 J_2
  <z_2 z_4> = -0.23888272452E-02 J_1 +  0.23894298138E-02 J_2
  <z_3 z_3> =  0.90331992406E-02 J_1 +   6.2215393203     J_2
  <z_3 z_4> =  0.12615619737E-02 J_1 +  0.86993437092     J_2
  <z_4 z_4> =  0.40889773929E-03 J_1 +  0.28190582528     J_2

  hit return to terminate program
\end{verbatim}
}
\subsection{Tracking a Spin Transformation: SLICK code  } \label{secslick}

In the theory of FPP, all spin calculations are formally identical to orbital calculations. This is explained in some details in \app{appspin}. The spin-orbital map can be normalised by a transformation which normalises the orbital part (first) and then the spin part. 

As in the orbital case, it is possible to track a canonical transformation. This is useful in a design phase when one attempts to match lattice functions. We now show, in the lowest order how it can be done with FPP and a code linked to it, namely PTC. 

According to \eq{normalspimoan1}, which we rewrite here

%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!T^f_""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" "$Monaco^:$ <c!$1(#$^:!m^f_,L$^S^f|
%|_}}: ;bP;/":#;bP8 :" :#,F:!,]:#,F:" <c%"C^<cY A)%# b'4<c!$1(#|
%|:!I,LA}}:"&c0  .P<c%"C^<cY(!('<c%"C^<cY(!(#<c!$1(#$^:!&c0!*a^|
%|'b!L_,LI}}:"&c0  .P<c!$1(#$^:!&c0!*a("n'b!L}_,LI}}}}_)*# b(<:#-<|
%|mbox-[-<normalsize ,D<c!$1(#$^:!a^f_,LI}}:#,D-]|
%|}# b D b!( b!L!WW}}:"&c0  .P<c!$1(#:!&c0!*r,L$^e_(":"&c0  q$^|
%|:!&c0!*L^y_}}}:"&c0  .P<c!$1(#$^:!&c0!*a("n'b!L}(",M1},LI}}:"&c0  .P|
%|<c!$1(#$^:!&c0!*a^'b!L(",M1},LI}}}}_)*# b'4:#-<mbox-[-<normalsize|
%| ,D<c!$1(#$^:!m^f_,L$^e_($:"&c0  q.P$^:!&c0!*a^f(",M1}$^L^y_}}}|
%|:#,D-]}& b!( b"0 b#8 b$@ b%H b&P!WW}}:"&c0  .P<c!$1(#:!&c0!*I|
%|,L$^A_(",M1}}}}& b!( b"0 b#8 b$@ b%H b&P!WW}}):# b'4:$Map around|
%| the parameter dependent fixed point:#-<mbox-[-<normalsize ,D|
%|:$ ,] <c!$1(#$^:!m^f_,L$^S^f_}}:#,D-]}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|_}: ;8/<:!;bP8normalspimoan0: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{T}_{f} &=
& ~\left({{m}_{f},{S}_{f}}\right)\nonumber \\
  &=
& \underbrace{\left({I,A}\right)\circ \overbrace{\overbrace{\left({{a}_{\ell},I}\right)\circ \left({{a}_{n\ell},I}\right)}\limits_{}^{\mbox{\normalsize $\left({{a}_{f},I}\right)$}}\circ \left({r,{e}^{\theta {L}_{y}}}\right)\circ \left({{a}_{n\ell}^{-1},I}\right)\circ \left({{a}_{\ell}^{-1},I}\right)}\limits_{}^{\mbox{\normalsize $\left({{m}_{f},{e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}}\right)$}}\circ \left({I,{A}^{-1}}\right)}\limits_{{\rm M}{\rm a}{\rm p}~{\rm a}{\rm r}{\rm o}{\rm u}{\rm n}{\rm d} ~{\rm t}{\rm h}{\rm e}~{\rm p}{\rm a}{\rm r}{\rm a}{\rm m}{\rm e}{\rm t}{\rm e}{\rm r}~{\rm d} {\rm e}{\rm p}{\rm e}{\rm n}{\rm d} {\rm e}{\rm n}{\rm t}~{\rm f}{\rm i}{\rm x}{\rm e}{\rm d} ~{\rm p}{\rm o}{\rm i}{\rm n}{\rm t}\mbox{\normalsize $~=~\left({{m}_{f},{S}_{f}}\right)$}}^{}\label{normalspimoan0}\end{eqnarray}
%
the map can be factorised as  a centre part which orbits around a locally vertical  invariant spin field (or ISF):
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"T^f_"#Symbol^:# :!,F,]<c!$1(#:"I,LA}}:#&c0  .P<c!$1(#|
%|$^:"&c0!*m^f_,L$^e_($:#&c0  q.P$^:"&c0!*a^f(",M1}$^L^y_}}}:#&c0  .P|
%|<c!$1(#:"&c0!*I,L$^A_(",M1}}}: ;8/<:";bP8slickmap: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {T}_{f} &=\left({I,A}\right)\circ \left({{m}_{f},{e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}}\right)\circ \left({I,{A}^{-1}}\right)\label{slickmap}\end{align}
%If we have an approximation of $\left({I,A}\right)$, for example its values on the closed orbit $\left({I,A_0}\right)$, we can construct the following map:
Now, as in the orbital case, let us look at the map at  locations $s=1$ and $s=2$:
%
%]|Expr|[#b @`b___})b [# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"T^1_"#Symbol^:# :!,F,]<c!$1($:"I,L$^A^1_}}:#&c0  .P<c!$1|
%|(#$^:"&c0!*m^1_,L$^S^1_}}:#&c0  .P<c!$1(#:"&c0!*I,L$^A^1(",M1}}}|
%|: ;bP;/":!;bP8 $^"$Times|:$T^2_:!,F,]$^:"T(!12}_:#&c0  .P$^:"&c0!*T|
%|^1_:#&c0  .P$^:"&c0!*T(!12}(",M1}: ;bP;/":!;bP8 ,F:#&c0  /?: ;bP;&c0!*/"|
%|:!;bP8 ,F,]$^:"T(!12}_:#&c0  .P<c!$1(#:"&c0!*I,L$("A}^1_}}:#&c0  .P|
%|<c!$1(#$^:"&c0!*m^1_,L$^S^1_}}:#&c0  .P<c!$1(#:"&c0!*I,L$^A^1|
%|(",M1}}}:#&c0  .P$^:"&c0!*T(!12}(",M1}: ;bP;/":!;bP8 ,F,]<c!$1|
%|(&:"I,L$^S(!12}_$^A^1_:#&c0  .P$^:"&c0!*m(!12}_}}:#&c0  .P<c!$1|
%|(#$^:"&c0!*m(!12}_,LI}}:#&c0  .P<c!$1(#$^:"&c0!*m^1_,L$^S^1_}}|
%|:#&c0  .P<c!$1(#$^:"&c0!*m(!12}(",M1},LI}}:#&c0  .P<c!$1(#:"&c0!*I|
%|,L$^<c!$1($$^S(!12}_$^A^1_:#&c0  .P$^:"&c0!*m(!12}_}}_(",M1}}}|
%|: ;bP;/":!;bP8 ,F,]<c!$1(&:"I,L$^S(!12}_$^A^1_:#&c0  .P$^:"&c0!*m|
%|(!12}_}}:#&c0  .P<c!$1(%$^:"&c0!*m^2_,L$^S^1_:#&c0  .P$^:"&c0!*m|
%|(!12}(",M1}}}:#&c0  .P<c!$1(#:"&c0!*I,L$^<c!$1($$^S(!12}_$^A^|
%|1_:#&c0  .P$^:"&c0!*m(!12}_}}_(",M1}}}: ;8/<:";bP8slickmapt: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
%\begin{align} {T}_{1} &=\left({I,{A}_{1}}\right)\circ \left({{m}_{1},{S}_{1}}\right)\circ \left({I,{A}_{1}^{-1}}\right)\nonumber \\
% {T}_{2}&={T}_{12}\circ {T}_{1}\circ {T}_{12}^{-1}\nonumber \\
% &\Downarrow \nonumber \\
% &={T}_{12}\circ \left({I,{A}_{1}}\right)\circ \left({{m}_{1},{S}_{1}}\right)\circ \left({I,{A}_{1}^{-1}}\right)\circ {T}_{12}^{-1}\nonumber \\
% &=\left({I,{S}_{12}{A}_{1}\circ {m}_{12}}\right)\circ \left({{m}_{12},I}\right)\circ \left({{m}_{1},{S}_{1}}\right)\circ \left({{m}_{12}^{-1},I}\right)\circ \left({I,{\left({{S}_{12}{A}_{1}\circ {m}_{12}}\right)}^{-1}}\right)\nonumber\\
% &=\left({I,{S}_{12}{A}_{1}\circ {m}_{12}}\right)\circ \left({{m}_{2},{S}_{1}\circ {m}_{12}^{-1}}\right)\circ \left({I,{\left({{S}_{12}{A}_{1}\circ {m}_{12}}\right)}^{-1}}\right)\label{slickmapt}\end{align}
%
%]|Expr|[#b @`b___})b G# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"T^1_"#Symbol^:# :!,F,]<c!$1($:"I,L$^A^1_}}:#&c0  .P<c!$1|
%|(#$^:"&c0!*m^1_,L$^S^1_}}:#&c0  .P<c!$1(#:"&c0!*I,L$^A^1(",M1}}}|
%|: ;bP;/":!;bP8 $^"$Times|:$T^2_:!,F,]$^:"T(!12}_:#&c0  .P$^:"&c0!*T|
%|^1_:#&c0  .P$^:"&c0!*T(!12}(",M1}: ;bP;/":!;bP8 ,F:#&c0  /?: ;bP;&c0!*/"|
%|:!;bP8 ,F,]$^:"T(!12}_:#&c0  .P<c!$1(#:"&c0!*I,L$("A}^1_}}:#&c0  .P|
%|<c!$1(#$^:"&c0!*m^1_,L$^S^1_}}:#&c0  .P<c!$1(#:"&c0!*I,L$^A^1|
%|(",M1}}}:#&c0  .P$^:"&c0!*T(!12}(",M1}: ;8/<:";bP8slickmapt: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {T}_{1} &=\left({I,{A}_{1}}\right)\circ \left({{m}_{1},{S}_{1}}\right)\circ \left({I,{A}_{1}^{-1}}\right)\nonumber \\
 {T}_{2}&={T}_{12}\circ {T}_{1}\circ {T}_{12}^{-1}\nonumber \\
 &\Downarrow \nonumber \\
 &={T}_{12}\circ \left({I,{A}_{1}}\right)\circ \left({{m}_{1},{S}_{1}}\right)\circ \left({I,{A}_{1}^{-1}}\right)\circ {T}_{12}^{-1}\label{slickmapt}\end{align}


\Eq{slickmapt} shows that the spin part of the canonical transformation to the  ISF normal form is trackable like all normal form transformations. Here is the piece of code, from \htm{slick_spin.f90}, which uses this fact to imitate the code SLICK.
\begin{verbatim}
if(mis) then
 if(ic==2) then 
  ang=-pi/2
 else
  ang=0
 endif
 call find_a(N0_AXIS,id,ang,n0,m0,l0)
else
  if(ic==2) then 
   n0=(/0,1,0/)
   m0=(/1,0,0/)
   l0=(/0,0,-1/)
 else
   n0=(/0,0,1/)
   m0=(/1,0,0/)
   l0=(/0,1,0/)  
 endif

  do j=1,3
  id%s%s(j,2)=n0(j)
  enddo
  do j=1,3
  id%s%s(j,3)=m0(j)
  enddo
  do j=1,3
  id%s%s(j,1)=l0(j)
  enddo
endif
\end{verbatim}
If the paramater {\tt mis} is true, the code will use FPP to compute the vectors $\vec{m}_0$ and $\vec{l}_0$ on the basis of an angle chosen by the user. If mis is false, the vectors are provided by the user.

Now a few words about the SLICK formalism are in order. The code SLICK propagates the spin matrix expressed, {\it perturbatively}, around a chosen input $\vec{n}_0$. It does so to first order in the transverse variables. Suppose the output $\vec{n}_0$ was on the desired direction, then the dependence of orbital part expresses a leading order mismatch. 

In PTC, this can be easily reproduced by tracking the canonical transformation which brings us into normal form, i.e.,  $\vec{n}_0$ along the $y$-axis and the two other vectors pointing in directions chosen by the user. In the example program \htm{slick_spin.f90}, this is done as follows:
\begin{verbatim}
closed_orbit=0.d0 ! initial orbit

RAY_CLOSED=closed_orbit

 ! ray= closed orbit + Canonical  transformation to (n_0,m_0,l_0)
 ray=RAY_CLOSED+id;  

CALL TRACK_PROBE(PSR,RAY,STATE,FIBRE1=1) ! Map is computed via the ray

m=ray

id=m.cut.2
id%m=1  ! just the spin part needs to be present

id=id**(-1) ! Removing the constant part of n_0

m=id*m  
\end{verbatim}
After the ray is tracked, the constant  part of the spin is removed.  The operation {\tt id=m.cut.2} removes the quadratic part in the orbital and  transverse dependence in the spin matrix.
The final map {\tt m} is the orbital map and the spin map around the tracked $\vec{n}_0$. It is a simple matter to read off the coefficients of the SLICK matrix by remembering that the ISF direction is the $y$-axis. The $\vec{m}_0$ and $\vec{l}_0$ axes are respectively the $z$ and $x$ axis of the spin matrix. 


This is done with the code:
\begin{verbatim}
do i=1,6
 je=0
 je(i)=1
w(8,i)=m%s%s(1,2).sub.je
w(7,i)=m%s%s(3,2).sub.je
enddo
\end{verbatim}

This concludes my discussion on the SLICK matrix: please run the code and see what you get.

\subsection{Coefficients of the Invariants: duality with Moments } 

\subsection{Complete construction of the $\delta $ dependent matrix with the moments
and the tunes } 



\chapter{\color{.} The Phase advance }\label{chaphasead}

\section{\color{.} General Theory: the effect of putting on a pair of glasses}

In Hamiltonian perturbation theory, the concept of the phase advance   cannot separated from normal form theory. In Hamiltonian perturbation theory, a canonical transformation $a(z;s)$ of phase space  will transform the Hamiltonian $H$ into a new Hamiltonian $K$ follows
%
%]|Expr|[#b @`b___})2# b'4" Chicago^: ;bP8&c0!*,^"!*|:!K""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" :!H:"&c0  .P:!&c0!*a ,K :"&c0  D:!&c0!*H|
%| : ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}K &=
& H\circ a\ +\ \Delta H\ \end{eqnarray}
%

The function $\Delta H$ depends on the derivative of $a$ with respect to the time-like variable ``$s$'' and is zero if that derivative  vanishes. The exact form of $\Delta H$ is not important here. 

In Hamiltonian theory, the normal form is specified by requiring that the  transformation $a$ be periodic and computed so as to make $K$ a function of the phase space radii only. Specifically, $K$ must have the form
%
%]|Expr|[#b @`b___})=# b'4" Chicago^: ;bP8&c0!*,^"!*|:!K<c!$1(#|
%|z,[s}}""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" :!K<c!$1(#$^J^k_|
%|,[s}}:" : ;8/<:!;bP8normh: ;8/=:#;bP8-<-<:" "$Monaco^:$where |
%|$^:!J^k_:" :#,F:!,]:#,F:" <2(#$^:!z($2k,M1}^2,K$^z("2k}^2}^2}|
%|,]<2($$^q($2k,M1}^2,K$^p("2k}^2}^2}: ,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}K\left({z;s}\right) &=
& K\left({{J}_{k};s}\right) \label{normh}\\ {\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~{J}_{k} &=
& {{z}_{2k-1}^{2}+{z}_{2k}^{2} \over 2}=
{{q}_{2k-1}^{2}+{p}_{2k}^{2} \over 2}\end{eqnarray}
%
The map between some position $s_1$ and $s_2$ is explicitly a rotation  if the normal form is of the form of \eq{normh}. In Hamiltonian perturbation theory, it would be hardly necessary to devote a section on this topic.

The same result will apply to spin if we recognize that the normal form for the spin is an equation of the form:
%
%]|Expr|[#b @`b___})b B# b'4" Chicago^: ;bP8&c0!*,^<2(""!*|:!d|
%|<c$%!^s}}(!ds}}""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" $^&c0  w|
%|^:!&c0!*y_<c!$1(#$^J^k_,[s}}$^L^y_<c$%!^s}: ;8/<:!;bP8normspin|
%|: ;8/= "$Monaco^:$       where <c$%!^s},]<c!$1(%$^s^"%#304^:%;bP8x|
%|_:$;8,L$^s^:%;bP8y_:$;8,L$^s^:%;bP8z_}}:$ and <c!1!^<c$%!^:!s}}|
%|,]1: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{d\vec{s} \over ds} &=
& {\omega }_{y}\left({{J}_{k};s}\right){L}_{y}\vec{s}\label{normspin} ~~~~~~~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~\vec{{\rm s}}=\left({{{\rm s}}_{x},{{\rm s}}_{y},{{\rm s}}_{z}}\right)~{\rm a}{\rm n}{\rm d} ~\left|{\vec{s}}\right|=
1\end{eqnarray}
%
The spin map between  $s_1$ and $s_2$ will be a rotation around the ``$y$'' axis.

To the practitioners of Hamiltonian perturbation theory, the phase advance and the normal form are truly identical concepts: the normalized Hamiltonian is the generator of infinitesimal rotations between the  points $s$ and $s+ds$. 

What is the story in a tracking code   dealing with the normalization of a one-turn map? This    is the topic of the sections which follow.

\subsection{\color{.} Phase advance: the need for more maps}\label{subphad}

The normalization algorithm in a code is based on the prime importance of the one-turn map, say from position $s=0$ back to $s=0$. In the simplest case  we know from the linear treatment  that we expect the normal form to be a rotation in each degree of freedom. In the general nonlinear case, we expect the normal form to be an amplitude dependent ($J_k$-dependent ) rotation in each plane.
The transformation $a(z)$, to the extent that it is ``convergent'' enough, will given us the (non)linear lattice functions, averages  and the invariants as it does in the linear case.

Anyone who has even been around accelerator physicists will testify to the necessary but insufficient nature of the one-turn map. Even the simplest of collider, where the collision point is  certainly of central importance, one needs to examine the beam at various locations around the ring. Experimentally beam position monitors (BPM) are installed for this purpose. Theoretically we also need to observe the beam at a myriad of points for all sorts of reasons which are beyond the subject of this document.

Therefore let us imagine two points: $s=0$ and $s=1$. Assuming that we can normalize  the one-turn map, we can certainly do it at these two locations: 
%
%]|Expr|[#b @`b___})b C# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!m^0|
%|_""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" $^:!a^0_:"&c0  .P:!&c0!*r|
%|:"&c0  .P$^:!&c0!*a^0(",M1}"$Monaco^:$    and    $^:!m^1_:" :!,]|
%|:" $^:!a^1_:"&c0  .P:!&c0!*r:"&c0  .P$^:!&c0!*a^1(",M1}: ;8/<|
%|:#two01: /=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{m}_{0} &=
& {a}_{0}\circ r\circ {a}_{0}^{-1}~~~~{\rm a}{\rm n}{\rm d} ~~~~{m}_{1} =
 {a}_{1}\circ r\circ {a}_{1}^{-1}\label{two01}\end{eqnarray}
%
The first important fact is the uniqueness  of the map $r$: this obvious property extends to the nonlinear case if all the maps are symplectic. If we allow nonsymplectic transformation, it is possible to alter the functional dependence of the nonlinear part of a rotation; this is not considered here.\footnote{Notice that we also assumed that different planes are not trivially swapped amongst each other. This means that linear eigenplanes are once and for all defined by their tunes which are assumed to be all distinct.} 

Additionally, since the tracking code can produce results at $0$ and $1$, we can consider the map $m_{01}$ which we also assume to be symplectic. This allows us to construct the following map called $r_{01}$:
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!r(!01}|
%|_""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" $^:!a^1(",M1}:"&c0  .P|
%|$^:!&c0!*m(!01}_:"&c0  .P$^:!&c0!*a^0_: ;8/<:#;bP8ph01: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{r}_{01} &=
& {a}_{1}^{-1}\circ {m}_{01}\circ {a}_{0}\label{ph01}\end{eqnarray}
%
The map $r_{01}$ expresses the transformation between normalized data at $s=0$ to normalised data at $s=1$. Since this data moves on circles, it is a transformation from circles to circles. If the reader accepts the statement that symplectic maps do not change the sizes of  circles, as previously stated, then sizes of the circles at $s=0$ and $s=1$ are the same. Thus the map $r_{01}$ can at most rotate a particle around these circles.

Hence \eq{ph01} defines the phase advance for a given choice of the canonical transformation $a$.

\subsection{\color{.}  On the lack of uniqueness of the phase advance}\label{subuniph} 

The phase advance is far from being unique. This can be seen by introducing a ``useless'' phase space rotation in the definition of the normal forms in \eq{two01}.
%
%]|Expr|[#b @`b___})b B# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!m^0|
%|_""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" <c%"C^<cY A(#$^:!a^0_|
%|:"&c0  .P$^:!&c0!*r^0_}}^$^b^0__}:"&c0  .P:!&c0!*r:"&c0  .P$^|
%|:!&c0!*r^0(",M1}:"&c0  .P$^:!&c0!*a^0(",M1}"$Monaco^:$ and $^|
%|:!m^1_:" :!,]:" <c%"C^<cY A(#$^:!a^1_:"&c0  .P$^:!&c0!*r^1_}}|
%|^$^b^1__}:"&c0  .P:!&c0!*r:"&c0  .P$^:!&c0!*r^1(",M1}:"&c0  .P|
%|$^:!&c0!*a^1(",M1}: ;8/<:#two01, ex: /=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{m}_{0} &=
& \underbrace{{a}_{0}\circ {r}_{0}}\limits_{{b}_{0}}^{}\circ r\circ {r}_{0}^{-1}\circ {a}_{0}^{-1}~{\rm a}{\rm n}{\rm d} ~{m}_{1} =
 \underbrace{{a}_{1}\circ {r}_{1}}\limits_{{b}_{1}}^{}\circ r\circ {r}_{1}^{-1}\circ {a}_{1}^{-1}\label{two01ex}\end{eqnarray}
%
Because the maps $b_0$ and $b_1$ also diagonalize their respective one-turn maps, we can use them in redefining \eq{ph01}:
%
%]|Expr|[#b @`b___})b C# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!r(!|
%|01}^""Symbol^:"&c0  .B&c0!* "#Helvetica|:#,F:!,]:#,F:" $^:!r^|
%|1(",M1}:"&c0  .P$^:!&c0!*a^1(",M1}:"&c0  .P$^:!&c0!*m(!01}_:"&c0  .P|
%|$^:!&c0!*a^0_:"&c0  .P$^:!&c0!*r^0_: ;bP;/":#;bP8 :" :#,F:!,]|
%|:#,F:" $^:!r^1(",M1}:"&c0  .P$^:!&c0!*r(!01}_:"&c0  .P$^:!&c0!*r|
%|^0_ : ;8/<:#;bP8ph01b: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{r}_{01}^{\prime } &=
& {r}_{1}^{-1}\circ {a}_{1}^{-1}\circ {m}_{01}\circ {a}_{0}\circ {r}_{0}\nonumber \\
  &=
& {r}_{1}^{-1}\circ {r}_{01}\circ {r}_{0}\ \label{ph01b}\end{eqnarray}
%
The reader can deduce two simple corollaries:

\begin{enumerate}
\item  The phase advance depends on the choice for $a$: this is obvious from \eq{ph01b}.
\item\label{itemidmap} If the map $a(s)$ is a function of the one-turn map at $s$ and nothing else, then the phase advance between matched locations will not depends on the choice of $a(s)$.
Matched location have identical one turn maps, i.e., identical invariants.
\end{enumerate}

\subsection{\color{.}  Tracking $a(s)$ and the phase advance}\label{subtracka}

The one-turn maps at positions 0 and 1 are connected through $m_{01}$:
%
%]|Expr|[#b @`b___})3# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!m^0_""Helvetica|:" |
%|,F:!,]:",F  $^:!m(!01}(",M1}"#Symbol^:#&c0  .P$^:!&c0!*m^1_:#&c0  .P|
%|$^:!&c0!*m(!01}_ : ;8/<:"twomaps01: /=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{m}_{0} &=
&  {m}_{01}^{-1}\circ {m}_{1}\circ {m}_{01}\ \label{twomaps01}\end{eqnarray}
%
We substitute \eq{twomaps01} in \eq{two01}:
%
%]|Expr|[#b @`b___})b O# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!m(!|
%|01}(",M1}""Symbol^:"&c0  .P$^:!&c0!*m^1_:"&c0  .P$^:!&c0!*m(!|
%|01}_:" "#Helvetica|:#,F:!,]:#,F:" $^:!a^0_:"&c0  .P:!&c0!*r:"&c0  .P|
%|$^:!&c0!*a^0(",M1}: ;bP;/":#;bP8 :" :#,F:"&c0  /?:#&c0!*,F:" |
%|: ;bP;/":#;bP8 :" $^:!m^1_:" :#,F:!,]:#,F:" <c%"C^<cY A(#$^:!m|
%|(!01}_:"&c0  .P$^:!&c0!*a^0_}}^$^b^1__}:"&c0  .P:!&c0!*r:"&c0  .P|
%|$^:!&c0!*a^0(",M1}:"&c0  .P$^:!&c0!*m(!01}(",M1}:" : ;8/<:#two|
%|01ph: /=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{m}_{01}^{-1}\circ {m}_{1}\circ {m}_{01} &=
& {a}_{0}\circ r\circ {a}_{0}^{-1}\nonumber \\
  &\Downarrow & \nonumber \\
  {m}_{1} &=
& \underbrace{{m}_{01}\circ {a}_{0}}\limits_{{b}_{1}}^{}\circ r\circ {a}_{0}^{-1}\circ {m}_{01}^{-1} \label{two01ph}\end{eqnarray}
%
%
The map $b_1 = {m}_{01}\circ {a}_{0}$  normalizes the map $m_1$ but is different from $a_1$. Looking at \eq{ph01} we see that 
%
%]|Expr|[#b @`b___})2# b'4" Chicago^: ;bP8&c0!*,^$^"!Times|:!b|
%|^: 1_,]$^""*|:"a^1_"#Symbol^:#&c0  .P$^:"&c0!*r(!01}_:# :",]:# |
%|$^:"m(!01}_:#&c0  .P$^:"&c0!*a^0_: ;8/<"$Helvetica|:$;bP8ph01|
%|t: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{b}_{1}={a}_{1}\circ {r}_{01} =
 {m}_{01}\circ {a}_{0}\label{ph01t}\end{eqnarray}
%
%
From \eq{ph01t} provides a very useful interpretation of the phase advance: the phase advance is the amount one must rotate the ``tracked'' value of $b_1$ into the $a_1$ one would obtain by simply running our normalization algorithm on the one-turn map at $s=1$.

\Eq{ph01t} is at the root of all ``Twiss'' loops in codes which use a normalization algorithm and operator overloading for Taylor series calculation. We summarize the procedure:

\begin{enumerate}
\item\label{itm1} One finds the closed orbit at $s=0$ and the one-turn map: this is done typically with a single overloaded tracking  command where the initial ray is the closed orbit plus the Taylor identity map.
\item The resulting map is then normalized: we get $r$ and $a_0$.
\item The initial ray is initialized as the closed orbit plus $a_0$ rather than the identity. 
\item The new ray is tracked from $s=i$ to $s=i+1$ all around the machine. At every step we get  %
%
%]|Expr|[#b @`b___})+# b'4" Helvetica|: ;bP8&c0!*,D$^"!Times|:!b|
%|(#: i,K1}_""Chicago^:",]$^"#*|:#a(#i,K1}_"$Symbol^:$&c0  .P$^|
%|:#&c0!*r(%i i,K1}_,]$^m(%i i,K1}_:$&c0  .P$^:#&c0!*a^i_: ,D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
${b}_{i+1}={a}_{i+1}\circ {r}_{i\ i+1}=
{m}_{i\ i+1}\circ {a}_{i}$.
%
 Lattice functions, whatever they might be, are extracted from $b_{i+1}$ and the phase advance from  ${r}_{i\ i+1}$.
\end{enumerate}
  
\subsection{\color{.}  Is there a special choice $a(s)$?}\label{subphadch}

Mathematically, particularly in a code, there is no preferential choice for   $a(s)$. However there are some choices which facilitate analytical calculations. This is certainly true for the linear normalizations. 

The most famous choice for the transformation $a(s)$ is the Courant-Snyder choice. Since we are dealing with a linear map, we can simply look at its  matrix. 
%
%]|Expr|[#b @`b___}),# b'4" Chicago^: ;bP8&c0!*,^"!*|:!A""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" <c!$1^["" ^<b R^$^&c0  b^:!&c0!*s_|
%|_}^0($,M$^:"&c0  a^:!&c0!*s_,O<b R^$^:"&c0  b^:!&c0!*s__}}(#1|
%|,O<b R^$^:"&c0  b^:!&c0!*s__}}}}: ;8/<:!;bP8acs: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}A &=
& \left({\begin{matrix}\sqrt {{\beta }_{s}}&0\\
-{\alpha }_{s}/\sqrt {{\beta }_{s}}&1/\sqrt {{\beta }_{s}}\end{matrix}}\right)\label{acs}\end{eqnarray}
%

The first obvious advantage of this choice is that it changes the position minimally; a normalized $q$ is simply the non-normalized $q$ divided $\sqrt {{\beta }_{s}}$. Thus it is a simple result of analytical perturbation theory to show that two thin sextupoles separated by a phase advance of $\pi $ (as defined by $\eq{acs}$), can be made to cancel exactly. This is not the case if one chooses  the following transformation for $a(s)$:
%
%]|Expr|[#b @`b___}),# b'4" Chicago^: ;bP8&c0!*,^"!*|:!A""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" <c!$1^["" (#:!1,O<b R^$^:"&c0  g^:!&c0!*s|
%|__}}($,M$^:"&c0  a^:!&c0!*s_,O<b R^$^:"&c0  g^:!&c0!*s__}}^0^|
%|<b R^$^:"&c0  g^:!&c0!*s__}}}: ;8/<:!;bP8acsi: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}A &=
& \left({\begin{matrix}1/\sqrt {{\gamma }_{s}}&-{\alpha }_{s}/\sqrt {{\gamma }_{s}}\\
0&\sqrt {{\gamma }_{s}}\end{matrix}}\right)\label{acsi}\end{eqnarray}
%
%
If the phase advance is defined through \eq{acsi} then it is no longer true that ``sextupoles $\pi $ apart'' cancel; physics is not changed, but physical results involving potentials which depend on the position $q$ cannot be expressed so concisely. Again we must emphasize that this is mostly an annoyance for analytical theory and for ``text book'' descriptions. 

There is another virtue to the Courant-Snyder definition of the phase advance: it corresponds to the phase difference between beam position monitors and it is  directly measurable with BPMs. Consider the   displacement of a particle at $s=1$ after $n$ turns expressed in terms of the  initial canonical variables at $s=0$:
%
%]|Expr|[#b @`b___})b Y# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!z_^|
%|1<c!$1^n}""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" $^:!M(!01}_$^|
%|M^0^n$^z_^0<c!$1^0}: ;bP;/":#;bP8 :" :#,F:!,]:#,F:" $^:!A^1_$|
%|^R(!01}_$^A^0(",M1}$^A^0_$^R^0^n$^A^0(",M1}$^z_^0<c!$1^0}:# : ;bP;/"|
%|:#;bP8 ,F:!,]:#,F:" $^:!A^1_$^R(!01}_$^R^0^n$^A^0(",M1}$^z_^0|
%|<c!$1^0} : ;bP;/":#;bP8 ,F:!,]:#,F:" $^:!A^1_$^A^0(",M1}<c%"C|
%|^<cY A($$^A^0_$^R(!01}_$^R^0^n$^A^0(",M1}}}(%"$Monaco^:$De Moivre|
%| applicable}_}$^:!z_^0<c!$1^0}: ;8/<:!;bP8bpm01: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{z}^{1}\left({n}\right) &=
& {M}_{01}{M}_{0}^{n}{z}^{0}\left({0}\right)\nonumber \\
  &=
& {A}_{1}{R}_{01}{A}_{0}^{-1}{A}_{0}{R}_{0}^{n}{A}_{0}^{-1}{z}^{0}\left({0}\right) \nonumber \\
 &=
& {A}_{1}{R}_{01}{R}_{0}^{n}{A}_{0}^{-1}{z}^{0}\left({0}\right)\ \nonumber \\
 &=
& {A}_{1}{A}_{0}^{-1}\underbrace{{A}_{0}{R}_{01}{R}_{0}^{n}{A}_{0}^{-1}}\limits_{{\rm D}{\rm e}~{\rm M}{\rm o}{\rm i}{\rm v}{\rm r}{\rm e}~{\rm a}{\rm p}{\rm p}{\rm l}{\rm i}{\rm c}{\rm a}{\rm b}{\rm l}{\rm e}}^{}{z}^{0}\left({0}\right)\label{bpm01}\end{eqnarray}
%
We can apply De Moivre's formula  (see \app{appdemoivre} ) to re-express \eq{bpm01} as a function of the  lattice functions at $s=0$:
%
%]|Expr|[#b @`b___})b!"# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!z_^|
%|1<c!$1^n}""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" $^:!A^1_$^A^0|
%|(",M1}<c!$1^["" ((:#-<cos<c!$1^$^:"&c0  F^:!&c0!*n)!# b(<c|
%|}# b D b!( b!L!WW}},K$^:"&c0  a^:!&c0!*0_:#-<sin<c!$1^$^:"&c0  F|
%|^:!&c0!*n)!# b(<c}# b D b!( b!L!WW}}}($$^:"&c0  b^:!&c0!*0_:#-<|
%|sin<c!$1^$^:"&c0  F^:!&c0!*n)!# b(<c}# b D b!( b!L!WW}}}(%,M$|
%|^:"&c0  g^:!&c0!*0_:#-<sin<c!$1^$^:"&c0  F^:!&c0!*n)!# b(<c|
%|}# b D b!( b!L!WW}}}((:#-<cos<c!$1^$^:"&c0  F^:!&c0!*n)!# b(<|
%|c}# b D b!( b!L!WW}},M$^:"&c0  a^:!&c0!*0_:#-<sin<c!$1^$^:"&c0  F|
%|^:!&c0!*n)!# b(<c}# b D b!( b!L!WW}}}}}$^z_^0:" : ;8/<:!;bP8bpm|
%|01n: ;8/=:#;bP8-<-<:" :# :" :#,F:!,]:#,F:" <c!$1^["" ("<b R^<2|
%|^$^&c0  b^:!&c0!*1_^$^:"&c0  b^:!&c0!*0_}_} }^0(",M<2^<c!$1(#|
%|$^:"&c0  a^:!&c0!*1_,M$^:"&c0  a^:!&c0!*2_}}^<b R("$^:"&c0  b|
%|^:!&c0!*1_$^:"&c0  b^:!&c0!*0_}_}}}^<b R^<2^$^:"&c0  b^:!&c0!*0|
%|_^$^:"&c0  b^:!&c0!*1_}_}}}<c!$1^["" (.:#-<vphantom-[<b R^<2^|
%|$^:"&c0  b^:!&c0!*1_^$^:"&c0  b^:!&c0!*0_}_}:#-] -<cos<c!$1^$|
%|^:"&c0  F^:!&c0!*n)!# b(<c}# b D b!( b!L!WW}},K$^:"&c0  a^:!&c0!*0|
%|_:#-<sin<c!$1^$^:"&c0  F^:!&c0!*n)!# b(<c|
%|}# b D b!( b!L!WW}}}($$^:"&c0  b^:!&c0!*0_:#-<sin<c!$1^$^:"&c0  F|
%|^:!&c0!*n)!# b(<c}# b D b!( b!L!WW}}}(*:#-<vphantom-[<b R^<2^|
%|$^:"&c0  b^:!&c0!*1_^$^:"&c0  b^:!&c0!*0_}_}:#-]:!,M$^:"&c0  g|
%|^:!&c0!*0_:#-<sin<c!$1^$^:"&c0  F^:!&c0!*n)!# b(<c|
%|}# b D b!( b!L!WW}}}((:#-<cos<c!$1^$^:"&c0  F^:!&c0!*n)!# b(<|
%|c}# b D b!( b!L!WW}},M$^:"&c0  a^:!&c0!*0_:#-<sin<c!$1^$^:"&c0  F|
%|^:!&c0!*n)!# b(<c}# b D b!( b!L!WW}}}}}$^z_^0: .O;bP;/""$Monaco^:$;bP8 |
%|where $^:"&c0  F^:!&c0!*n)!# b'4c}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|:" :#,F:!,]:#,F:" :!n:"&c0  m:!&c0!* ,K $^:"&c0  m(!:!&c0!*01}|
%|)!# b'4c}& b!( b"0 b#8 b$@ b%H b&P!WW}:$     here $^:"&c0  m(!|
%|:!&c0!*01})!# b'4c}& b!( b"0 b#8 b$@ b%H b&P!WW}:$ results from|
%| :#-<eq-[acs-] : .O,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{z}^{1}\left({n}\right) &=
& {A}_{1}{A}_{0}^{-1}\left({\begin{matrix}\cos\left({{\Phi }_{n}^{c}}\right)+{\alpha }_{0}\sin\left({{\Phi }_{n}^{c}}\right)&{\beta }_{0}\sin\left({{\Phi }_{n}^{c}}\right)\\
-{\gamma }_{0}\sin\left({{\Phi }_{n}^{c}}\right)&\cos\left({{\Phi }_{n}^{c}}\right)-{\alpha }_{0}\sin\left({{\Phi }_{n}^{c}}\right)\end{matrix}}\right){z}^{0} \label{bpm01n}\\   &=
& \left({\begin{matrix}\sqrt {{{\beta }_{1} \over {\beta }_{0}}}\ &0\\
-{\left({{\alpha }_{1}-{\alpha }_{2}}\right) \over \sqrt {{\beta }_{1}{\beta }_{0}}}&\sqrt {{{\beta }_{0} \over {\beta }_{1}}}\end{matrix}}\right)\left({\begin{matrix}\vphantom{\sqrt {{{\beta }_{1} \over {\beta }_{0}}}} \cos\left({{\Phi }_{n}^{c}}\right)+{\alpha }_{0}\sin\left({{\Phi }_{n}^{c}}\right)&{\beta }_{0}\sin\left({{\Phi }_{n}^{c}}\right)\\
\vphantom{\sqrt {{{\beta }_{1} \over {\beta }_{0}}}}-{\gamma }_{0}\sin\left({{\Phi }_{n}^{c}}\right)&\cos\left({{\Phi }_{n}^{c}}\right)-{\alpha }_{0}\sin\left({{\Phi }_{n}^{c}}\right)\end{matrix}}\right){z}^{0}\nonumber 
\nonumber \\
~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~{\Phi }_{n}^{c} &=
& n\mu \ +\ {\mu }_{01}^{c}~~~~~{\rm h}{\rm e}{\rm r}{\rm e}~{\mu }_{01}^{c}~{\rm r}{\rm e}{\rm s}{\rm u}{\rm l}{\rm t}{\rm s}~{\rm f}{\rm r}{\rm o}{\rm m}~\eq{acs} \nonumber 
\end{eqnarray}
%
%
%
%
The final is step is simply to write the position $q$ at $s=1$:
%
%]|Expr|[#b @`b___})0# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!q_^1,H|
%|n,I""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" <b R^<2^$^&c0  b^:!&c0!*1|
%|_^$^:"&c0  b^:!&c0!*0_}_}<c!$1)-# b'4:#-<vphantom-[<b R^<2^$^|
%|:"&c0  b^:!&c0!*1_^$^:"&c0  b^:!&c0!*0_}_}:#-]<c!$1((-<cos<c!$1|
%|^$^:"&c0  F^:!&c0!*n)!# b(<c}# b D b!( b!L!WW}},K$^:"&c0  a^:!&c0!*0|
%|_:#-<sin<c!$1^$^:"&c0  F^:!&c0!*n)!# b(<c|
%|}# b D b!( b!L!WW}}}}$^q^0_,K$^:"&c0  b^:!&c0!*0_:#-<sin<c!$1|
%|^$^:"&c0  F^:!&c0!*n)!# b(<c}# b D b!( b!L!WW}}$^p^0_|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}: ;8/<:!;bP8qn: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{q}^{1}(n) &=
& \sqrt {{{\beta }_{1} \over {\beta }_{0}}}\left({\vphantom{\sqrt {{{\beta }_{1} \over {\beta }_{0}}}}\left({\cos\left({{\Phi }_{n}^{c}}\right)+{\alpha }_{0}\sin\left({{\Phi }_{n}^{c}}\right)}\right){q}_{0}+{\beta }_{0}\sin\left({{\Phi }_{n}^{c}}\right){p}_{0}}\right)\label{qn}\end{eqnarray}
%

According to \eq{qn}, the  only difference  between $s=0$ and $s=1$ is the scaling factor $\sqrt {{{\beta }_{1} \over {\beta }_{0}}}$ and the ``Courant-Snyder'' phase ${\mu }_{01}^{c}$.
Therefore if one Fourier analyses the positions at  $s=0$ and $s=1$, the phase  ${\mu }_{01}^{c}$ will be directly measurable. 

This results extends to the coupled $x-y$ matrix provided that $A$ is chosen so as to satisfy
%
%]|Expr|[#b @`b___})=# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!A(!12}|
%|_,]$^A(!34}_,]0""Monaco^:"   with   $^:!A(!11}_,^0:"  and  $^|
%|:!A(!33}_,^0 : ;8/<:!;bP8xycs: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{A}_{12}=
{A}_{34}=
0~~~{\rm w}{\rm i}{\rm t}{\rm h}~~~{A}_{11}>0~~{\rm a}{\rm n}{\rm d} ~~{A}_{33}>0\ \label{xycs}\end{eqnarray}
%
In an electron machine, one can excite the beam with a small kick at a frequency corresponding, say to the tune ${\nu }_1$. The motion in the other eigen-plane will damp out.
If one then performs a measurement of the beam position at $s=0$ and $s=1$, the motion will be phase shifted by an amount corresponding to the Hamiltonian phase advance defined by \eq{ph01}.

Some readers may wonder if that corresponds to the so-called Teng-Edwards phase advance. The answer is yes. However we want to state emphatically that the factorization of $A$ into a pseudo-rotation and a Courant-Snyder transformation, as done by Teng-Edwards, is unnecessary and numerically useless. Some people have talked about the angle $\phi $ between the eigenplane: this is again a misconception. For arbitrarily small coupling, this number $\phi$ can  be pure imaginary! Thus the only necessary and useful condition to impose on the map $A$ is \eq{xycs}.

In conclusion, it is important to state that the special choice of a phase is mainly dictated by  analytical ease and the human brain: on the computer it does not matter what $A$ we choose except
for the linear transverse phase advance for which the Courant-Snyder-Teng-Edwards choice is convenient.


\section{\color{.}(Non)Linear Phase Advance: how to write a Twiss Loop}

\subsection{ \color{.}The fast and dirty way}\label{secphdirt}
In this section we implement the phase advance resulting from the choice given by \eq{xycs} as well as the choice I dubbed the ``anti-Courant-Snyder gauge'':
\begin{eqnarray}{A}_{21}=
{A}_{43}=
0~~~{\rm w}{\rm i}{\rm t}{\rm h}~~~{A}_{22}>0~~{\rm a}{\rm n}{\rm d} ~~{A}_{44}>0\ \label{xyacs}\end{eqnarray}
The choice of \eq{xyacs} would be useful in a world dominated by momentum measurements or momentum dependent perturbations. 
 In this section we check that the fractional part of the phase advance is the same between matched position. Our example is not very sophisticated: we use a lattice made of 10 identical cells.
It would have been better to select a lattice containing sections with internal differences   which have been matched nevertheless: a cell and a phase trombone for example.  Anyway, this is not a lecture on matching....



 Here we use the simplest state of PTC: {\tt only_4d}. 
The algorithm described here is  general to the extent that it works with coupling; however it does not work with parameter dependence. This is fixed in \sec{secphdirtp}.

It is important to realise that it will produce the same phase advance as the Teng-Edwards formalism simply because \eq{xycs} is also satisfied by Teng-Edwards.
One could perform the factorisation of Teng-Edwards but it is in my view an unnecessary and even evil\footnote{My comment on the evil nature of Teng-Edwards does not apply to analytical 
calculations which  are very much problem-dependent. It applies to a run of the mill computer Twiss cranking.} to do since it entails a   non-global definition--- discontinuous for large coupling.
In any event, it is available in FPP. 

The example code is located at \htm{phase_advance_dirty.f90}. It is a simple Twiss loop as used in \sec{seclatgenave}. However the following ``purification'' coding is added by
a call to a subroutine called {\tt purify(a_l,p,f)}:
{\footnotesize
\begin{verbatim}
   do i=1,2
    if(f==0) then  ! Courant-Snyder-Teng-Edwards A_12=0
          ind=0; ind(2*i-1)=1;
          a11=a_l%v(2*i-1).sub.ind
          ind=0; ind(2*i)=1;
          a12=a_l%v(2*i-1).sub.ind
          p(i)=atan2(a12,a11) 
    else           ! Anti Courant-Snyder A_21=0 
          ind=0; ind(2*i-1)=1;
          a21=a_l%v(2*i).sub.ind
          ind=0; ind(2*i)=1;
          a22=a_l%v(2*i).sub.ind
          p(i)=atan2(-a21,a22) 
    endif
 
    dri%v(2*i-1) = cos(p(i))*(1.0_dp.mono.(2*i-1))-sin(p(i))*(1.0_dp.mono.(2*i))
    dri%v(2*i)   = cos(p(i))*(1.0_dp.mono.(2*i))+sin(p(i))*(1.0_dp.mono.(2*i-1))
  enddo

    a_l=a_l*dri
\end{verbatim}
}
With $f=0$, the Courant-Snyder definition is implemented. With $f=1$, the ``anti-Courant-Snyder'' definition is implemented. The code compares the fractional  part of these
two  phase advances in the file  {\tt phase_difference.dat}. Incidentally, the ``anti-Courant-Snyder''  can produce a negative integer part in a normal lattice unlike the Courant-Snyder definition.

Below is the result when there are no errors in the lattice. Because the lattice is made of ten identical cells, the difference between phase advances is
periodic. Being zero at the beginning, which happens to be a symmetry point, it is zero at every symmetry point further down.
{\tiny
\begin{verbatim}
     s           Dphi_x      Dphi_y     
   0.0000       0.0000       0.0000     <-- symmetry point in ideal lattice 
   2.2865      0.76818E-01  0.43166E-01
   2.7865      0.20690     -0.22119    
   3.2365      0.22375     -0.21507    
   5.7859      0.27993     -0.25493    
   6.2359      0.28636     -0.23922    
   6.7359     -0.41779E-01 -0.78732E-01
   9.0224      0.11102E-15 -0.24980E-15 <-- symmetry point in ideal lattice 
   11.309      0.76818E-01  0.43166E-01
   11.809      0.20690     -0.22119       
                  .
                  .
                  .
   78.415      0.28636     -0.23922    
   78.915     -0.41779E-01 -0.78732E-01
   81.202     -0.88818E-15 -0.88818E-15 <-- symmetry point in ideal lattice 
   83.488      0.76818E-01  0.43166E-01
   83.988      0.20690     -0.22119    
   84.438      0.22375     -0.21507    
   86.988      0.27993     -0.25493    
   87.438      0.28636     -0.23922    
   87.938     -0.41779E-01 -0.78732E-01
   90.224     -0.44409E-15 -0.88818E-15 <-- symmetry point in ideal lattice 
\end{verbatim}
}
In the presence of misalignments the periodicity is broken: only the total phase advance remains independent of choice of canonical transformation. Here are the results:
{\tiny
\begin{verbatim}
     s           Dphi_x      Dphi_y     
   0.0000       0.0000       0.0000     <-- symmetry point in ideal lattice 
   2.2865      0.76819E-01  0.43166E-01
   2.7865      0.20690     -0.22119    
   3.2365      0.22375     -0.21507    
   5.7859      0.27993     -0.25494    
   6.2359      0.28636     -0.23923    
   6.7359     -0.41775E-01 -0.78736E-01
   9.0224      0.42805E-05 -0.74017E-05 <-- symmetry point in ideal lattice 
   11.309      0.76819E-01  0.43159E-01
                  .
                  .
                  .
   78.415      0.28636     -0.23923    
   78.915     -0.41778E-01 -0.78737E-01
   81.202      0.12682E-05 -0.53280E-05 <-- symmetry point in ideal lattice 
   83.488      0.76819E-01  0.43161E-01
   83.988      0.20690     -0.22119    
   84.438      0.22375     -0.21507    
   86.988      0.27993     -0.25493    
   87.438      0.28636     -0.23922    
   87.938     -0.41779E-01 -0.78733E-01
   90.224      0.13323E-14  0.44409E-15 <-- symmetry point in ideal lattice 
\end{verbatim}
}

The above algorithm implements the computation ${r}_{01}$ in \eq{ph01t} (for Courant-Snyder-Teng-Edwards) on the assumption that the map is linear and that condition \eqe{xycs} determines $a_1$.
In the main Twiss loop, the polymorphic ray ({\tt RAY(6)}) is constantly upgraded so as  to be always the ``(Anti)-Courant-Snyder-Teng-Edwards'' choice.  The rotation needed for this upgrade is the phase advance. This is the relevant  fragment of \htm{phase_advance_dirty.f90}:
%
\v5
{
\begin{verbatim}

 ray=a_l+closed_orbit

 call track(psr,ray,k,k+1,state)

 a_l=ray

!!!!  Algorithm to force the linear A into the !!!!
!!!!  form A_12=A_34=0  if ph=0                !!!!
!!!!  form A_21=A_43=0  if ph=1                !!!!
      call purify(a_l,p,ph)
!!!!          End of the Algorithm             !!!!   
 
    closed_orbit=ray

    phase_advance=phase_advance+p
\end{verbatim}
}





{\bf \color{red} N.B. in \htm{phase_advance_dirty.f90}, after the initial normal form on the one-turn map, the map  $a$ ({\tt normal\%a_t}) is also purified  into the ``(Anti)-Courant-Snyder-Teng-Edwards'' choice. In reality it is not needed in my standard FPP normalisation
as long as there are no parameters. But the ``expert user'' should not trust my normal form whose sole claim is to turn a messy phase space into circles. And indeed,
as we will see in \sec{secphdirtp}, this first ``purification'' is needed if parameter dependence is present.
}


\subsection{ \color{.}The not-so-fast and dirty way to handle parameters}\label{secphdirtp}

The code is located at \htm{phase_advance_dirty_parameter.f90}. The only major difference is that the phase advance must be type Taylor. The code which ``purifies''
the canonical transformation is now:

{\footnotesize
\begin{verbatim}
type(taylor), intent(inout) :: p(2)
type(damap), intent(inout) :: a_l
type(damap) dri
type(taylor) a11,a12
integer ind(4),i,f

call alloc(dri); call alloc(a11,a12);

   do i=1,2
   ind=0; ind(2*i-1)=1;
   a11=(a_l%v(2*i-1).par.ind)
   ind=0; ind(2*i)=1;
   a12=(a_l%v(2*i-1).par.ind)
 
          p(i)=atan2(a12,a11) 

    dri%v(2*i-1) = cos(p(i))*(1.0_dp.mono.(2*i-1))-sin(p(i))*(1.0_dp.mono.(2*i))
    dri%v(2*i)   = cos(p(i))*(1.0_dp.mono.(2*i))  +sin(p(i))*(1.0_dp.mono.(2*i-1))
  enddo

    a_l=a_l*dri
\end{verbatim}
}

The example in \htm{phase_advance_dirty_parameter.f90} allows the user to skip the purification of the map coming out of the normal form: the result will be wrong for the delta dependence of the phase advance (chromaticities). As I said before, my normal form is somewhat arbitrary except for the linear part. For the linear  calculation, which is ubiquitous, I impose the Courant-Snyder condition on the full  $4\times 4$ coupled matrix.

Should one impose a stricter (or more correct) definition of $a$ in the normal form  algorithm? In practice, we rarely need the phase advance beyond a simple linear calculation,i.e., for the usual Twiss table. To impose a stricter definition would slow down the normalization without any specific need.

Morevoer, what gauge should we use for the normalisation transformation? This is something to be decided by the user who will write his own routine with or without the help of an expert.
For example, if spin is included, there are many possibilities for the spin normal form transformation. In the work of Desmond Barber (DESY), the spin phase advance is decided through the selection of a pair of vectors $\vec l$ and $\vec m$ perpendicular to the invariant spin axis  $\vec n$. The natural choice in a normal form using Lie methods differs from that of Barber. Without getting into a useless  debate, it would be quite reasonable for a user to compare a simple lattice with FPP with a calculation using formulae derived by Barber. In that case, if the phase advance is compared, Barber's choice must be implemented, and thus, the user is expected to write his own purification routine.

So in general, this is a two step process: normalise using your favourite package and then force $a$ into the desire form by rotating it-- orbital and spin. The rotation angles are the phase advances.

\subsection{\color{.}The more general (slow) way: linear and nonlinear}\label{secphnl}
In this section we simply point out, through the example \htm{phase_advance_general.f90} , that this entire theory works with nonlinearities.\footnote{It also works with spin but  is not included in the example program.}

The particular ``gauge'' chosen here for the nonlinear part of $a$ is the reversed-Dragt-Finn factorisation with no ``tune shift'' terms. The full tracked map $a$, more precisely its Lie representation, can be factorised as:
%
%]|Expr|[#b @`b___})2# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|: A:!,F,]$^: R(!""Symbol^:"&c0  Df}_:!&c0!*-<exp<c!$1(#"#*|:#,Z|
%|$^g(!no}_,Z}}'b M:!-<exp<c!$1(#:#,Z$^g^3_,Z}}$^: A(!:#cs}_:! |
%|: ;8/<:#;bP8adfa: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\cal A}&={{\cal R}}_{\Delta \phi }\exp\left({:{g}_{no}:}\right)\cdots\exp\left({:{g}_{3}:}\right){{\cal A}}_{cs} \label{adfa}\end{align}
In \eq{adfa} the rotation ${\cal R}_{\Delta \phi }$ is selected so that none of the Lie polynomials $g_k$ contain any tune shift terms. The map ${\cal A}_{cs}$ has already been forced into a Courant-Snyder form as in \sec{secphdirtp}.

There are no special reasons to select this special form for the nonlinear part except for the fact that it emerges naturally during the normal form algorithm.  As far as the code is concerned, it looks very much like that of \sec{secphdirtp} except that {\tt purify} is replaced by a general routine of FPP called {\tt CANONIZE}:
{
\begin{verbatim}
 ray=a_l+closed_orbit

 call track(psr,ray,k,k+1,state)

 a_l=ray

!!!!  Algorithm to force the linear A into the !!!!
!!!!  form A_12=A_34=0                         !!!!
 call CANONIZE( a_l,A_l,PHASE_ADVANCE)
!!!!          End of the Algorithm             !!!!   
 
    closed_orbit=ray

    phase_advance(1)=phase_advance(1)+p(1)/twopi
    phase_advance(2)=phase_advance(2)+p(2)/twopi
\end{verbatim}
}

{\bf \color{red} N.B. We do not recommend looking into the routine {\tt CANONIZE} of FPP because of the lacunae we discussed previously. The absence of a complex TPSA makes this routine difficult to understand. Hopefully we will provide this routine with the complex FPP at a later date.
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%








\chapter{ \color{.} Tracking a stochastic quadratic envelope with radiation}\label{chaprad}

This is also discussed in \app{secenvelope} in connection with the complex FPP.  The example program used here  is \htm{radiation.f90}.

\section{ \color{.} Getting beam sizes with this exact linear theory}\label{secsize}

The first  step consists in 
finding the  deterministic closed orbit in the presence of (classical) radiation. This is done with the code fragment:
 
\begin{verbatim}

state=default0+radiation0+envelope0

closed_orbit=0.d0 ! initial guess for closed orbit

call FIND_ORBIT_probe_x(ALS,closed_orbit,state,1.d-5,fibre1=1)

write(6,'(a16,6(1x,g12.5))') " closed orbit = ",closed_orbit(1:6)

\end{verbatim}

It is important to realise that PTC mimics the real machine: if there are no RF cavities in the lattice, the {\tt FIND_ORBIT_probe_x} command will crash
because there is no mechanism to regain the energy lost through radiation.

The next step is to track the object of type {\tt probe_8} called ray_8  around this deterministic closed orbit:

\begin{verbatim}

RAY_CLOSED=closed_orbit

id=1;   ray=RAY_CLOSED+id;   ! ray= closed orbit + identity map

CALL TRACK_PROBE(ALS,RAY,STATE,FIBRE1=1) ! One turn map is computed via the ray
\end{verbatim}

The object ray_8 is defined as

\begin{verbatim}

  type probe_8
     type(real_8) x(6)     ! Polymorphic orbital ray
     type(spinor_8) s(ISPIN0R:ISPIN1R)   ! Polymorphic spin s(1:3)
     type(rf_phasor_8) AC  ! Modulation of magnet
     real(dp) E_ij(6,6)   !  Envelope for stochastic radiation
     !   stuff for exception
     logical u
     type(integration_node),pointer :: lost_node
  end type probe_8
\end{verbatim}
and contains two objects of central importance for a stochastic map:  {E_ij(6,6)} and x(6). The deterministic part $M$ of \eq{mommapr}, influenced by classical radiation, will be extracted  from  the polymorph x(6).
The translation of the stochastic envelope is in the arrays  E_ij called $E^{PTC}$ in \eq{mommapr}. 

%
%]|Expr|[#b @`b___})5# b'4" Chicago^: ;bP8&c0!*.["!Helvetica|:! |
%|$^""Symbol^:"&c0  S_^"#*|:#&c0!*1:!,F,]M<c!$1(#$^:"&c0  S_^:#&c0!*0|
%|,K$^E_(!PTC}}}$^:!M_^: t:! : ;8/<:#;bP8mommapr: ;8/=:";bP8 :!-<-<|
%|:" $^&c0  S(!:#&c0!*ij}_:!,F,]<c!,Q("$^z^i_$^z^j_}}: ;8/<:!;bP8momdefr|
%|: ;8/=;bP8.K}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{subequations}\begin{align} {\Sigma}^{1}&=M\left({{\Sigma}^{0}+{E}^{PTC}}\right){M}^{{\rm T}} \label{mommapr} \\ {\Sigma}_{ij}&=\left\langle{{z}_{i}{z}_{j}}\right\rangle\label{momdefr}\end{align}\end{subequations}
%
\eq{mommapr} represents the map as it appears in the tracking code. This is converted into a damapspin by the line:

\begin{verbatim}
 
 m=ray   ! The ray is "officially" turned into a "damapspin"  (DA in Berz's honour)

\end{verbatim}
In the damapsin, the stochastic E_ij is redefined:
%
%]|Expr|[#b @`b___})># b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  S_^"#*|:#&c0!*1:!,F,]M<c!$1(#$^:"&c0  S_^:#&c0!*0|
%|,K$^E_(!PTC}}}$^:!M_^: t:! : ;bP;/":!;bP8 ,F,]M$^:"&c0  S_^:#&c0!*0|
%|$^:!M_^: t:#,K<c%"C^<cY A(#:!M$^:#E_(!PTC}$^:!M_^: t}}^:#E_}: ;bP;/"|
%|:!;bP8 ,F,]M$^:"&c0  S_^:#&c0!*0$^:!M_^: t:#,KE : ;8/<:#;bP8damapmom|
%|: ;8/=:!;bP8 : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\Sigma}^{1}&=M\left({{\Sigma}^{0}+{E}^{PTC}}\right){M}^{{\rm T}} \nonumber \\
 &=M{\Sigma}^{0}{M}^{{\rm T}}+\underbrace{M{E}^{PTC}{M}^{{\rm T}}}\limits_{E}^{}\nonumber \\
 &=M{\Sigma}^{0}{M}^{{\rm T}}+E\ \label{damapmom} \end{align}
The equilibrium beam sizes are the solution of the equation:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  S_^.E:!&c0!*,F,]M$^:"&c0  S_^.E$^:!&c0!*M_^|
%|: t"#*|:#,KE:! : ;8/<:#;bP8equildd: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\Sigma}^{\infty }&=M{\Sigma}^{\infty }{M}^{{\rm T}}+E \label{equildd}\end{align}
\eq{equildd} can be solved by a matrix inversion: a $21 \times 21$ matrix for the moments needs to be inverted. The results are in the array
{\tt normal\%s_ij0(i,j)}; see the print loop in the example program.

However this is not how FPP or the complex FPP proceeds. The procedure involves phasors which bring us closer to the theory of synchrotron integrals.
This is the topic of the next section.

\section{ \color{.} Getting the approximate Chao synchrotron integral   from 
\protect\ref{secsize}}\label{secchao}

The map $M$ can be put into a complex phasors basis by a two-step process. First the map is transformed into a ``damped'' rotation by a transformation which is analogous to the Courant-Snyder transformation. In the absence of radiation, this transformation is symplectic.

Secondly, the map is diagonalised by going into the standard phasors' basis. Thus we have:
%
%]|Expr|[#b @`b___})8# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|M,]B""Symbol^:"&c0  L: &c0!*$^:!B_(",M1}"#Monaco^:#       where|
%| :"&c0  L:!&c0!*,]<c!$1^[$$ ^$^e_(%"$*|:$,M$^:"&c0  a^:$&c0!*1|
%|_,Mi$^:"&c0  m^:$&c0!*1_}^:!0^:$'b M^:!0^0^$^e_(%:$,M$^:"&c0  a|
%|^:$&c0!*1_,Ki$^:"&c0  m^:$&c0!*1_}^'b M^:!0^:$'b M^'b M^:!'b!<|
%|^0^0^0^0^$^e_(%:$,M$^:"&c0  a^:$&c0!*3_,Ki$^:"&c0  m^:$&c0!*3|
%|_}}}: ;8/<:!diagdamp: /=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} M=B\Lambda {B}^{-1}~~~~~~~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~\Lambda =\left({\begin{matrix}{e}^{-{\alpha }_{1}-i{\mu }_{1}}&0&\cdots&0\\
0&{e}^{-{\alpha }_{1}+i{\mu }_{1}}&\cdots&0\\
\cdots&\cdots&&0\\
0&0&0&{e}^{-{\alpha }_{3}+i{\mu }_{3}}\end{matrix}}\right)\label{diagdamp}\end{align}
We then change the basis in which the moments are expressed:
%
%]|Expr|[#b @`b___})-# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""Symbol^:"&c0  s:!&c0!*,F,]$^"#*|:#B_(",M1}:"&c0  S$^:#&c0!*B|
%|_($: t :#,M1}:! : ;8/<:!momphas: /=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \sigma &={B}^{-1}\Sigma{B}^{{\rm T} -1} \label{momphas}\end{align}
\eq{momphas} diagonalises \eq{equildd}
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  s_^.E:!&c0!*,F,]:"&c0  L$^s_^.EL"#*|:#&c0!*,K|
%|:"&c0  e:!&c0!* : ;bP;/":!;bP8 :"&c0  />:#&c0!* $^:"&c0  s(!:#&c0!*ij}|
%|^:"&c0  .E:!&c0!*,F,]<2^1($1,M$^:"&c0  L(!:#&c0!*ii}_$^:"&c0  L|
%|(!:#&c0!*jj}_}}$^:"&c0  e(!:#&c0!*ij}_: ;8/<:#;bP8equilddp: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\sigma }^{\infty }&=\Lambda {\sigma }^{\infty }\Lambda +\varepsilon  \nonumber \\
 \Rightarrow \ {\sigma }_{ij}^{\infty }&={1 \over 1-{\Lambda }_{ii}{\Lambda }_{jj}}{\varepsilon }_{ij}\label{equilddp}\end{align}
Of course, the original beam sizes are obtained by transforming back into the original Cartesian frame. This is done directly in the complex FPP and by some trickery in the real FPP.

The reader will notice that ${\varepsilon }_{ij}$, which contains the stochastic information, has 21 independent terms. Thus it seems that 21 synchrotron integrals are needed to compute the equilibrium beam sizes. However it is well known that only 3 such functions are usually computed. In the theory of Chao, there is one so-called H-function for each plane, thus 3 integrals. The theory of Sands, which assumes a special role for the third plane, has two H-functions and an equilibrium energy. How does this emerge from \eq{equilddp}?

To see the emergence of the simplified Chao theory, we need to look at two characteristic terms, say  ${\sigma }^{\infty }_{12}$ and  $ {\sigma }^{\infty }_{13}$. Let us start with $ {\sigma }^{\infty }_{13}$:
%
%]|Expr|[#b @`b___})+# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  s(!"#*|:#&c0!*13}^:"&c0  .E:!&c0!*,F,]<2^1(%|
%|1,M-<exp<c!$1(%:#,M<c!$1($$^:"&c0  a^:#&c0!*1_,K$^:"&c0  a^:#&c0!*2|
%|_}},Mi<c!$1(#$^:"&c0  m^:#&c0!*1_,K$^:"&c0  m^:#&c0!*2_}}}}}}|
%|$^:"&c0  e(!:#&c0!*13}_: ;8/<:#;bP8equilddp13: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\sigma }_{13}^{\infty }&={1 \over 1-\exp\left({-\left({{\alpha }_{1}+{\alpha }_{2}}\right)-i\left({{\mu }_{1}+{\mu }_{2}}\right)}\right)}{\varepsilon }_{13}\label{equilddp13}\end{align}
%
and compare \eq{equilddp13} with the equation for ${\sigma }^{\infty }_{12}$:
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  s(!"#*|:#&c0!*13}^:"&c0  .E:!&c0!*,F,]<2^1(%|
%|1,M-<exp<c!$1(#:#,M2$^:"&c0  a^:#&c0!*1_}}}}$^:"&c0  e(!:#&c0!*12}|
%|_: ;bP;/":!;bP8 ,F<c$)A^:"-^}<2^:!1(":#$^:"&c0  a^:#&c0!*1_}}|
%|<2^$^:"&c0  e(!:#&c0!*12}_^2}: ;8/<:#;bP8equilddp12: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\sigma }_{13}^{\infty }&={1 \over 1-\exp\left({-2{\alpha }_{1}}\right)}{\varepsilon }_{12}\nonumber \\
 &\underline{\sim }{1 \over {\alpha }_{1}}{{\varepsilon }_{12} \over 2}\label{equilddp12}\end{align}
Away from linear resonances, the factor 
%
%]|Expr|[#b @`b___})## b'4" Helvetica|: ;bP8&c0!*,D<2^1(%1,M-<|
%|exp<c!$1(%"!*|:!,M<c!$1(#$^""Symbol^:"&c0  a^:!&c0!*1_,K$^:"&c0  a|
%|^:!&c0!*2_}},Mi<c!$1(#$^:"&c0  m^:!&c0!*1_,K$^:"&c0  m^:!&c0!*2|
%|_}}}}}}: ,D}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
${1 \over 1-\exp\left({-\left({{\alpha }_{1}+{\alpha }_{2}}\right)-i\left({{\mu }_{1}+{\mu }_{2}}\right)}\right)}$
in \eq{equilddp13} has a magnitude of order unity for small damping decrements. On the other hand, the similar factor in \eq{equilddp12} is of order $1/{\alpha }_1$. Since damping is very small  in an accelerator, it is clear that ${\sigma }^{\infty }_{12}$,${\sigma }^{\infty }_{34}$, and ${\sigma }^{\infty }_{56}$ will contribute most to the equilibrium beam sizes. Thus we can write:

%
%]|Expr|[#b @`b___})0# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  S(!"#*|:#&c0!*ab}^:"&c0  .E:!&c0!*,F<c$)A^:"-^}|
%|<c%#D(#$^:!B(!ai}_$^:"&c0  s(!:#&c0!*ij}^:"&c0  .E$^:!&c0!*B(!|
%|bj}_}("-<sum}(#:#i,Lj}_}: ;bP;/":!;bP8 ,F,]2<c!$1(+$^B("a1}_$|
%|^:"&c0  s(!:#&c0!*12}^:"&c0  .E$^:!&c0!*B("b2}_,K$^B("a3}_$^:"&c0  s|
%|(!:#&c0!*34}^:"&c0  .E$^:!&c0!*B("b4}_,K$^B("a5}_$^:"&c0  s(!|
%|:#&c0!*56}^:"&c0  .E$^:!&c0!*B("b6}_}} : ;8/<:#;bP8sizeemit: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\Sigma}_{ab}^{\infty }&\underline{\sim }\sum\limits_{i,j}^{} {B}_{ai}{\sigma }_{ij}^{\infty }{B}_{bj}\nonumber \\
 &=2\left({{B}_{a1}{\sigma }_{12}^{\infty }{B}_{b2}+{B}_{a3}{\sigma }_{34}^{\infty }{B}_{b4}+{B}_{a5}{\sigma }_{56}^{\infty }{B}_{b6}}\right) \label{sizeemit}\end{align}

The quantities %
%]|Expr|[#b @`b___})## b'4" Helvetica|: ;bP8&c0!*,D$^"!Symbol^:!&c0  s|
%|('""*|:"&c0!*2i,M1 2i}^:!&c0  .E: &c0!*,D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
${\sigma }_{2i-1\ 2i}^{\infty }$
are proportional (twice as big) to the so-called equilibrium emittances. 

In the real FPP we have preferential access to the real canonical transformation $A$ rather than the complex transformation $B$ of \eq{sizeemit}; therefore the code fragment for the computation of
$<x^2>$ and  $<t^2>$ uses the Ripken lattice function and the Chao emittances. The fragment is:
 
\begin{verbatim}
 x2=0.d0
 t2=0.d0
 a=normal%n%a_t
 do i=1,3
  x2=x2+(a(1,2*i-1)**2+a(1,2*i)**2)*normal%emittance(i)
  t2=t2+(a(6,2*i-1)**2+a(6,2*i)**2)*normal%emittance(i)
 enddo
\end{verbatim}

The result is 
\begin{verbatim}
 <x^2> =   3.90719632549390290E-008  <-- computed with envelopes
 <x^2> =   3.90719691464787583E-008  <-- computed with emittances
 <t^2> =   1.87704234716327040E-005  <-- computed with envelopes
 <t^2> =   1.87704433355895918E-005  <-- computed with emittances
\end{verbatim}

\noindent {\color{red} N.B.  The reader can uncomment the lines
 {\color{blue}
\begin{verbatim}
!do i=1,6                 ! uncomment to artificially increase the damping to 90%!!!
! ray%x(i)=.1d0*ray%x(i)  ! 
!enddo      
\end{verbatim}
}
\noindent and run the code again. The surprising result is
 {\color{blue}
\begin{verbatim}
 <x^2> =   3.30342851830113604E-014  <-- computed with envelopes
 <x^2> =   3.30283341529342826E-014  <-- computed with emittances
 <t^2> =   2.85509057419003280E-014  <-- computed with envelopes
 <t^2> =   1.83477783871648358E-011  <-- computed with emittances 
\end{verbatim}
}
So, what is going on here? The average of the transverse $x^2$ is wrong by about 1\%. The average of $t^2$ is wrong, as we would expect, by orders of magnitude since a damping of 90\% cannot be considered minute. The answer resides in the transformational properties of the stochastic array $E$. This array transforms like quadratic phasors. Since the ALS, the example ring used, is made of 12 identical cells, we expect phasors corresponding to resonances to rotate around the unit circle and partially cancel each other. The rotation rate is given by the phase of the resonance corresponding to each cell. Thus the array $E$ has a natural tendency to align itself with the shape of the trajectory of the map $M$, i.e., the former invariants of the deceased symplectic map.
In the longitudinal plane, there is no such symmetry because the cavity is only located at a single place in the ring and also no cancellation is possible since the overall phase advance is minute: thus the smallness of the damping is critical to the Chao theory as well as the more approximate Sands theory in the longitudinal plane.
}


%\section{ Getting the even more approximate Sands $H_1$ and $H_2$ from \protect\ref{secchao} }\label{secsands}
%\section{Nonlinear? Tracking non-stochastic moments }


\chapter{The one-resonance Normal Form }\label{chares}

\section{The Trivial method of Dragt for 1-d resonance}

\section{The  method of Turchetti and Forest for higher dimensionality}
The technique used here is described in \cite{thebook} starting at page 141. The theory of Turchetti is described in terms of rotational properties of the normalised Taylor maps for those who want to look at it. Here we follow my approach which I published with John Irwin as an SSC note long time ago. It is closer to the Hamiltonian method and, with vector fields, it is totally equivalent to Turchetti's approach since the vector field is the Lie generator  of a general Taylor map. 

\subsection{\color{.} Example of the Turchetti Theory in 1-d}

The program \htm{one_resonance.f90} contains the case of a $3Q_x$ and $4Q_x$ resonance carried to second order in sexupole strength. The example lattice is again the ALS.
In this example, the tunes are fitted to be close to the selected resonance.

We first perform a normal form but we instruct the code to leave multiples of the resonances in the map. In the case of the $3Q_x$ resonances, we have in the code

\begin{verbatim}
j=1
do i=3,c_%no,3
 normal%nres=j
 normal%m(1,j)=3*j;
 j=j+1
enddo
\end{verbatim}
This is followed by a normal form. Using the canonical transformation normal\%a_t, the map is put into a ``single-resonance normal form.'' The code fragment is very simple:

\begin{verbatim}
 normal = m  ! The map is normalised into a single resonance  
 m=normal%a_t**(-1)*m*normal%a_t
\end{verbatim}

The normalised map cannot be connected connected to the identity all over phase space. This means that there cannot be a pseudo-Hamiltonian (Lie operator) which represents the motion near the origin, in the islands and past the islands. This is topologically impossible.

However, it is possible to rotate the map in the plane of the resonance and produce a map which is {\it near the identity} and has the same invariant phase space as the original map to the order in perturbation theory. As usual no claims of convergence are made. This is done in the code fragment: 

\begin{verbatim}
hrot=pi/3.d0*((1.d0.mono.'2')+(1.d0.mono.'02'))
m=texp(hrot,m)
\end{verbatim}

In mathematical language, this  translates into:
%
%]|Expr|[#b @`b___})3# b'4" Chicago^: ;bP8&c0!*,^"!*|:!h""Helvetica|:",F,]|
%|<2^"#Symbol^:#&c0  p^:!&c0!*3},Z$^x_^2,K$^p^x^2,Z: ;bP;/":";bP8 |
%|: M:",F:#&c0  .Z:"&c0!*-<exp<c!$1(#:!,Zh,Z}}: M,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}h&={\pi  \over 3}:{x}^{2}+{p}_{x}^{2}:\nonumber \\
 {\cal M}&\equiv \exp\left({:h:}\right){\cal M}\end{eqnarray}
%
The new map $m$ is co-moving with the resonance. For multi-dimensional resonances, the theory is a bit less obvious, we encourage the reader to consult reference \cite{thebook}. 

The map $m$ can now be put into a single Lie operator by performing a diverging calculation. In the code fragment

\begin{verbatim}
normal%nres=0
normal=m  ! #1

hrot%h=normal%normal%pb%h*normal%a_t**(-1)  ! #2
hr=hrot
\end{verbatim}

In line \#1, the calculation produces a divergence in the map normal\%a_t. This divergence is cancelled in line \#2. The resulting function is the invariant. The function hr is the same invariant in resonance basis: in the complex FPP it would be a simple map transformation, in the real FPP some magic takes place under this equal sign. We are now in a position to write the full invariant in the phasors basis: it contains tune shifts and resonance driving turns only.

\subsection{ A 2-d example}

\section{Spin resonance: here the power of our methods outshines the rest!}

\subsection{Example with an ordinary resonance:~
%]|Expr|[#b @`b___})'# b'4" Helvetica|: ;bP8&c0!*,D$^"!Symbol^:!&c0  n|
%|^""*|:"&c0!*x_,K$^:!&c0  n(!"#Monaco^:#&c0!*spin}_:",]1: ,D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
${\nu }_{x}+{\nu }_{{\rm s}{\rm p}{\rm i}{\rm n}}=
1$
}

\subsection{Example with a magnet modulation resonance:~ 
%]|Expr|[#b @`b___})'# b'4" Helvetica|: ;bP8&c0!*,D$^"!Symbol^:!&c0  n|
%|(!""Monaco^:"&c0!*mo}_"#*|:#,K$^:!&c0  n(!:"&c0!*spin}_:#,]1: ,D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
${\nu }_{{\rm m}{\rm o}}+{\nu }_{{\rm s}{\rm p}{\rm i}{\rm n}}=
1$
}

\chapter{Miscellaneous Useful Stuff in FPP}

\section{ Dragt-Finn Representation}
\section{ One-Lie Exponent representation}
\section{ Mixed generating functions and their practical use (Symplectic tracking and BPM reconstruction)}
\section{  Forcing $A$ in a particular form to all orders: gives phase advance}
\section{  Symplectify maps or checking the symplectic condition}
\section{  Inverting general maps}
\section{ Solving Maxwell Equation in the bend with TPSA }

%\setcounter{chapter}{0}
%\Alph{chapter}
\begin{appendices}
\appendixpage
\noappendicestocpagenum
\addappheadtotoc

\chapter{\color{.} Normal Form of the Tiny Package}\label{apptiny}

 
\section{\color{.} Normal Form in General}\label{secpersonal}

Normal forms usually, but not always, are made of two distinct algorithms. 

\begin{enumerate}
\item The linear part of the map is exactly normalised. This in turns involves  two processes:
\begin{enumerate}
\item The linear  map is moved around its parameter dependent fixed point by the map {\tt a0}: {\tt{call find\_disp(m,a0)}}. 
\item The linear  part is turned into a rotation or a damped rotation (a sink) in the simplest case: {\tt call diag\_mat(N\%R,a1,n\%tune,n\%damping)} by the map {\tt a1}.
\end{enumerate}
\item The nonlinear part is normalised. This can include the parameter dependence of the linear part beyond first order in the parameter.
\item Finally, we have an optional process of forcing the entire transformation in ``a canonical'' form of our choice. Here the word canonical is very accurate: according to religious laws. 
The linear choice of Courant-Snyder is enforced as well as some nonlinear choice as well. This comes from the fact that transformations into a normal form are known up to a rotation, i.e., up to a member of the commutative group of the normal form. The difference between the canonical choice and the tracked transformation is the phase advance.
\end{enumerate}





\section{\color{.} Some explanations of the actual code}\label{secpartial}

This small example in the files  \htmt{y_my_own_da.f90} and   \htmt{y_my_own_analysis.f90} follows the steps of a regular ``professional'' code. It is restricted to a one degree of freedom (1-d-f) case with one parameter. The maximum order is 4.
This cannot cover the case of a Jordan normal form where transverse planes are oscillating and the longitudinal undergoes drift like motion. 

\subsection{\color{.} Linear fixed point: {\tt{find\_disp(m,a0)}}  }\label{secfix}
The linear map has the following form where $\delta $ is the parameter of our toy package:
%
%]|Expr|[#b @`b___})7# b'4" Chicago^: ;bP8&c0!*,^<c$%!^"!Symbol^:!&c0  h}|
%|""*|:"&c0!*,]$^<c!$1(#1,MM}}_(",M1}<c$%!^v}"#Monaco^:#  where|
%| $^<c$%!^:"z}_^f,]M<c$%!^z} ,K :!&c0  d<c$%!^:"&c0!*v} : ;8/<|
%|:";bP8eta: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}\vec{\eta }=
{\left({1-M}\right)}^{-1}\vec{v}~~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~{\vec{z}}^{f}=
M\vec{z}\ +\ \delta \vec{v}\ \label{eta}\end{eqnarray}
%
The transformation $a_0$ is given by:
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*,^$^"!Times|:!a|
%|^0_ ,Z <c$%!^""*|:"z} ,K "#Symbol^:#&c0  d<c$%!^h}:"&c0!* : ;8/<|
%|:";bP8a0: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{a}_{0}\ :\ \vec{z}\ +\ \delta \vec{\eta }\ \label{a0}\end{eqnarray}
%
If the old linear map is denoted by $m$, then the map $n$ defined as
%
%]|Expr|[#b @`b___})b @# b'4" Chicago^: ;bP8&c0!*,^"!Times|:!n|
%|,]$^a^0(",M1}""Symbol^:"&c0  .P:!&c0!*m:"&c0  .P$^:!&c0!*a^0_|
%|"#Monaco^:#    and    :!n,H<c$%!^"$*|:$z},L:"&c0  d:!&c0!*,I,]|
%|M<c$%!^:$z}:# for all :"&c0  d: &c0!*,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}n=
{a}_{0}^{-1}\circ m\circ {a}_{0}~~~~{\rm a}{\rm n}{\rm d} ~~~~n(\vec{z},\delta )=
M\vec{z}~{\rm f}{\rm o}{\rm r}~{\rm a}{\rm l}{\rm l}~\delta \end{eqnarray}
%
This is done by the following lines:
%
\begin{verbatim}
!@3 !!!!  Linear Analysis starts here
    call find_disp(m,a0)   !@3 &nbsp; Find closed orbit to first order in parameter

    n%a_t=a0
!@1 !!!!  The map is put around the liner fixed point
    N%R=A0**(-1)*m*A0
\end{verbatim}
\subsection{\color{.} Normalising the linear part: {\tt diag\_mat(m,a1,tune,damping) }}\label{seclin}

The diagonalization of the linear part, in general, requires finding the eigenfunction of the linear (Lie) map associated to the linear map $m$. Consider an arbitrary linear function $f$:
%
%]|Expr|[#b @`b___})+# b'4" Chicago^: ;bP8&c0!*,^"!Times|:!f<c!$1|
%|^<c$%!^""*|:"z}},]$^v^1_$^z^1_,K$^v^2_$^z^2_: ,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}f\left({\vec{z}}\right)=
{v}_{1}{z}_{1}+{v}_{2}{z}_{2}\end{eqnarray}
%
The map $m$ acts on $f$ in the usual way of a map transforming functions:
%
%]|Expr|[#b @`b___})># b'4" Chicago^: ;bP8&c0!*,^<c!$1(""!Times|:!m|
%|f}}<c!$1^<c$%!^""*|:"z}},]f<c!$1("M<c$%!^z}}}"#Symbol^:# "$Helvetica|:$,F|
%|:",]:$,F:# :" <c%#D)## b'4$^v^1_$^M("1i}_$^z^i_|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(":$-<sum}^:"i_},K$^v^2_$^M("2i}|
%|_$^z^i_: ;bP;/":$;bP8 :# :$,F:",]:$,F:# $^<c!$1("$^:"M_^: ,C:"v}}|
%|^1_$^z^1_,K$^<c!$1("$^M_^: ,C:"v}}^2_$^z^2_: ,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}\left({mf}\right)\left({\vec{z}}\right)=
f\left({M\vec{z}}\right) &=
& \ \sum\limits_{i}^{} {v}_{1}{M}_{1i}{z}_{i}+{v}_{2}{M}_{2i}{z}_{i}\nonumber \\
  &=
& {\left({{M}^{\dagger }v}\right)}_{1}{z}_{1}+{\left({{M}^{\dagger }v}\right)}_{2}{z}_{2}\end{eqnarray}
%
Thus we find the eigenvectors of $M^{\dagger}$.  This is done in the piece of code
\begin{verbatim}
!@1 first we find the eigenvectors which we assume here to be complex (stable) 
    
    mat=m
    
    det=mat(1,1)*mat(2,2)-mat(1,2)*mat(2,1)    
    trace=mat(1,1)+mat(2,2)
    
    del=i_*sqrt(abs(trace**2-4.0_dp*det))
    lam=(trace+del)/2.0_dp
    
!@1 we need an eigenvector of the transpose of mat to construct A    
    v(1)=1.0_dp
    v(2)=(lam-mat(1,1))/mat(2,1)
\end{verbatim}
In the stable case (non hyperbolic), the two eigenfunctions are complex conjugate and correspond to two eigenvalues on the unit circle in the symplectic case (near it in the radiative case). Thus if $f$ is an eigenfunction with eigenvalue 
%
%]|Expr|[#b @`b___})$# b'4" Helvetica|: ;bP8&c0!*,D-<exp<c!$1("|
%|"!*|:!i""Symbol^:"&c0  m}}: &c0!*,D}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
$\exp\left({i\mu }\right)$, its complex conjugate %
%]|Expr|[#b @`b___})## b'4" Helvetica|: ;bP8&c0!*,D<c$%A^"!*|:!f}|
%|: ,D}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
$\overline{f}$
is an eigenvector of eigenvalue $\exp\left({-i\mu }\right)$. Moreover the function $f \overline{f}$ is thus an invariant function of the map $m$. If we rewrite this invariant using the real and imaginary part of $f$
%
%]|Expr|[#b @`b___})-# b'4" Chicago^: ;bP8&c0!*,^"!*|:!f<c$%A^|
%|f}""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" $($"$Monaco^:$Re:!,H|
%|f,I}_^2,K$($:$Im,H:!f,I}^i^2: ,\:!}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}f\overline{f} &=
& {{\rm R}{\rm e}(f)}^{2}+{{\rm I}{\rm m}(f)}_{i}^{2}\end{eqnarray}
%
we can see the definition of the Floquet variables 
%
%]|Expr|[#b @`b___})=# b'4" Chicago^: ;bP8&c0!*,^"!Helvetica|:!,F,F|
%|$^""*|:"x_(!new},]<2($"#Monaco^:#Re:",Hf,I})!# b'4$^<c!=Q))# b'4|
%|:#Re:",Hf,I,L:#Im:",Hf,I}& b!( b"0 b#8 b$@ b%H b&P!WW}}_(#1,O|
%|2}}& b!( b"0 b#8 b$@ b%H b&P!WW}}:#   and   $^:"p^x(!new},]<2|
%|($:#Im:",Hf,I})"# b'4"$Symbol^:$&c0  s$^<c!=Q))# b'4:#&c0!*Re|
%|:",Hf,I,L:#Im:",Hf,I}& b!( b"0 b#8 b$@ b%H b&P!WW}}_(#1,O2}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}} : ;bP;/":!;bP8 ,F,F:#where :$&c0  s|
%|:"&c0!*,]:#sign of <c!=Q))# b'4Re:",Hf,I,L:#Im:",Hf,I|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}&&{x}^{new}=
{{\rm R}{\rm e}(f) \over {\left[{{\rm R}{\rm e}(f),{\rm I}{\rm m}(f)}\right]}^{1/2}}~~~{\rm a}{\rm n}{\rm d} ~~~{p}_{x}^{new}=
{{\rm I}{\rm m}(f) \over \sigma {\left[{{\rm R}{\rm e}(f),{\rm I}{\rm m}(f)}\right]}^{1/2}}\ \nonumber \\
 &&{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~\sigma =
{\rm s}{\rm i}{\rm g}{\rm n}~{\rm o}{\rm f}~\left[{{\rm R}{\rm e}(f),{\rm I}{\rm m}(f)}\right]\end{eqnarray}
%
Here follows the piece of code which produces the map $a_1$ which diagonalises the matrix.
\begin{verbatim}
!@1  Insuring that    Poisson bracket should be equal to one
    x(1)=real(v(1))
    x(2)=real(v(2))
    px(1)=aimag(v(1))
    px(2)=aimag(v(2))
    norm=x(1)*px(2)-x(2)*px(1)
    if(norm<0) then
     norm1= sqrt(abs(norm))
     norm2=-norm1
    else
     norm1= sqrt(abs(norm))
     norm2= norm1
    endif
    x=x/norm1
    px=px/norm2
!@1 Now the Poisson bracket should be equal to one    

    a1%v(1)= ( x(1).mono.1)+( x(2).mono.2)
    a1%v(2)= (px(1).mono.1)+(px(2).mono.2)
    
    a1=a1**(-1)
\end{verbatim}

\noindent  Comment \# 1: This does {\bf NOT } create a transformation of the Courant-Snyder type. For example, in the case of the toy lattice coming with our toy package, the map $a_1$  which normalises the linear map has the form 
 \begin{verbatim}
  variable 1
   (1,0,0) 0.6521598164755E+00
   (0,1,0) 0.2399778620767E+01
  variable 2
   (0,1,0) 0.1533366476647E+01
\end{verbatim}
That is to say, the $A_{12}$ entry of the matrix is {\tt 0.2399778620767E+01}  rather that zero. This is due to our choice for the eigenvector $f$. Obviously a religious subroutine called ``canonize'' well rectify this otherwise blasphemous choice of yours truly.   I preserve the choice $A_{12}=0$ (and  $A_{34}=0$ in the 2-d-f case) which has some experimental significance. The  non-global factorization of Teng-Edwards, in the coupled case, which blows up on a finite subset of SP(4), I generally ignore. 



\subsection{\color{.} Normalising the nonlinear part: {\tt analyse\_kernel}} \label{secnonlin}

This is actually quite independent of the previous discussion. It is a recursive process performed order by order. We start by assuming that the map is now a rotation to order $k$ in the Taylor series. 
%
%]|Expr|[#b @`b___})*# b'4" Chicago^: ;bP8&c0!*,^"!*|:!n,]nl""Symbol^:"&c0  .P|
%|:!&c0!*r: ;8/<:!;bP8ndef: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}n=
nl\circ r\label{ndef}\end{eqnarray}
%
where the map $r$ is the linear part of the  rotation and $nl$ is a rotation to order $k$ in the combined powers of $z_1,z_2$ and $\delta$. The first step is to go into the eigenvariables of the map $r$. This is done with the transformation
denoted in the code as {\tt from\_phasor} and its inverse {\tt to\_phasor}. Their matrix representations are:
%
%]|Expr|[#b @`b___})3# b'4" Chicago^: ;bP8&c0!*,^"!Monaco^:!to|
%|""Times|:"-?:!phasor"#*|:#,]<c!$1^["" ^1^i^1(",Mi}}}:!    from|
%|:"-?:!phasor:#,]<c!$1^["" (#1,O2}(#1,O2}($,Mi,O2}(#i,O2}}}: ;8/<|
%|:#;bP8fromto: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{\rm t}{\rm o}\_{\rm p}{\rm h}{\rm a}{\rm s}{\rm o}{\rm r}=
\left({\begin{matrix}1&i\\
1&-i\end{matrix}}\right)~~~~{\rm f}{\rm r}{\rm o}{\rm m}\_{\rm p}{\rm h}{\rm a}{\rm s}{\rm o}{\rm r}=
\left({\begin{matrix}1/2&1/2\\
-i/2&i/2\end{matrix}}\right)\label{fromto}\end{eqnarray}
%



$$\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_$$
{ 
\subsubsection{Phasors as eigenfunctions}
These matrices are   used to define two eigenfunctions, the phasors $h$ and $\overline{h}$
%
%]|Expr|[#b @`b___})b E# b'4" Chicago^: ;bP8&c0!*,^"!*|:!h,]x,K|
%|ip    ""Monaco^:"and:!     <c$%A^h},]x,Mip     -;h,L<c$%A^h}-=,],M|
%|2i: ;8/<:!;bP8pbpha: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}h=
x+ip\ \ \ \ {\rm a}{\rm n}{\rm d} \ \ \ \ \ \overline{h}=
x-ip\ \ \ \ \ [h,\overline{h}]=
-2i\label{pbpha}\end{eqnarray}
%
%
The phasors can be used to create eigenfunctions of the Lie map $\cal R$ associated to $r$ defined by substituting $r$ into a function:
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*,^R"!*|:!f,]f""Symbol^:"&c0  .P|
%|:!&c0!*r "#Helvetica|:#-<Longrightarrow ,H"$Times|:$rf,I,H<c$%!|
%|^z},I,]f<c!$1($r,H<c$%!^z},I}},]f<c!$1("R<c$%!^z}}}: ,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{\cal R}f=
f\circ r\ \Longrightarrow (rf)(\vec{z})=
f\left({r(\vec{z})}\right)=
f\left({R\vec{z}}\right)\end{eqnarray}
%
Thus we have:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*,^R$^"!*|:!h_^$|
%|^n^1_$^<c$%A^h}_^$^n^2_,]""Helvetica|:"-<exp<c!$1($:!,Mi<c!$1|
%|(#$^n^1_,M$^n^2_}}"#Symbol^:#&c0  m}}$^:!&c0!*h_^$^n^1_$^<c$%A|
%|^h}_^$^n^2_: ;8/<:!;bP8eigpha: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{\cal R}{h}^{{n}_{1}}{\overline{h}}^{{n}_{2}}=
\exp\left({-i\left({{n}_{1}-{n}_{2}}\right)\mu }\right){h}^{{n}_{1}}{\overline{h}}^{{n}_{2}}\label{eigpha}\end{eqnarray}
%
\subsubsection{Lie maps and Lie operators}

The map $\cal R$ is precisely a map which can be represented by Lie operators; it is a nonlinear Lie map. The trick of perturbation theory is to transform \eq{ndef} into an equation
involving Lie maps which simply act on an arbitrary function $f$ by substitution.
%
%]|Expr|[#b @`b___})9# b'4" Chicago^: ;bP8&c0!*,^N"!*|:!,]: R$|
%|^N^:!l_""Helvetica|:"-<Longrightarrow :! f"#Symbol^:#&c0  .P:!&c0!*nl|
%|:#&c0  .P:!&c0!*r,]: R<c!$1(#:!f:#&c0  .P:!&c0!*nl}},]: R$^N^|
%|:!l_f: ;8/<:!;bP8order: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{\cal N}=
{\cal R}{{\cal N}}_{l}\Longrightarrow \ f\circ nl\circ r=
{\cal R}\left({f\circ nl}\right)=
{\cal R}{{\cal N}}_{l}f\label{order}\end{eqnarray}
%
This last equation, \eq{order}, shows why Lie maps operate in the reverse order from matrix multiplication. In fact in the linear case, as we have already seen, they are represented
by the transposed of the matrix  used to track rays or linear moments. (Ergo different order as well!)
}
$$\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_$$
 \subsubsection{The recursive algorithm of the normal form}

\noindent  We now turn to the description of the algorithm. 

\noindent The map ${\cal N}_l$ is normalised up to order  $k$ 
%
%]|Expr|[#b @`b___})8# b'4" Chicago^: ;bP8&c0!*,^$^N^"!*|:!l_""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" :#-<exp<c!$1(#:!F:"&c0  /7/1}}: ;bP;&c0!*/"|
%|:#;bP8 :" :!F:#,F:!,]:#,F:" $^:!F(":"&c0  .C:!&c0!*k}_,K$^F(#|
%|k,K1}_,K'b M: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{{\cal N}}_{l} &=
& \exp\left({F\cdot \nabla }\right)\nonumber \\
  F&=
& {F}_{\le k}+{F}_{k+1}+\cdots\end{eqnarray}
%
The Lie operator to order $k$ is a rotation and/or sink\footnote{sink: rotation times a multiplication by a scalar.}
%
%]|Expr|[#b @`b___})3# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^<c!!!^$^""*|:"F(""#Symbol^:#&c0  .C:"&c0!*k}_}^1_:# :!,F,]:" |
%|<c%#D('i2n$^A(!nm}_$^h_^n$^<c$%A^h}_(#n,M1}$^:#&c0  d_^:"&c0!*m}|
%|(":!-<sum}('$^n^1_,K$^n^2_,Km:#&c0  .C:!&c0!*k}_}: ;bP;/":!;bP8 |
%|$^<c!!!^$^:"F(":#&c0  .C:"&c0!*k}_}^2_:# :!,F,]:# <c%#D((:",M|
%|i2n$^A(!nm}_$^h_(#n,M1}$^<c$%A^h}_^n$^:#&c0  d_^:"&c0!*m}(":!-<|
%|sum}('$^n^1_,K$^n^2_,Km:#&c0  .C:!&c0!*k}_}: ;8/<:";bP8fk: ;8/=|
%|:!;bP8 : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\left.{{F}_{\le k}}\right|}_{1} &=\ \sum\limits_{{n}_{1}+{n}_{2}+m\le k}^{} i2n{A}_{nm}{h}^{n}{\overline{h}}^{n-1}{\delta }^{m}\nonumber \\
 {\left.{{F}_{\le k}}\right|}_{2} &= \sum\limits_{{n}_{1}+{n}_{2}+m\le k}^{} -i2n{A}_{nm}{h}^{n-1}{\overline{h}}^{n}{\delta }^{m}\label{fk} \end{align}




The purpose of the routine {\tt analyse\_kernel} is to remove from  the polynomial ${F}_{k+1}$ the terms which are not pure sinks. This is done with a Lie transformation ${\cal B}=\exp\left({g\cdot \nabla }\right)$
%
%]|Expr|[#b @`b___})0# b'4" Chicago^: ;bP8&c0!*,^BN$^B_(""!*|:!,M|
%|1},]""Helvetica|:"-<exp<c!$1(#:!g"#Symbol^:#&c0  /7/1}}: &c0!*R|
%|$^N^"$Times|:$l_:"-<exp<c!$1($:!,Mg:#&c0  /7/1}}: ;8&c0!*/<:!;bP8maineq|
%|: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{\cal B}{\cal N}{{\cal B}}^{-1}=
\exp\left({g\cdot \nabla }\right){\cal R}{{\cal N}}_{l}\exp\left({-g\cdot \nabla }\right)\label{maineq}\end{eqnarray}
%
The equation for $g$, please consult references (or \sec{expl}), is given by:
%
%]|Expr|[#b @`b___})0# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"g^1_:!,F:",]<c%#D($<2^$^G(!nm}_((1,M:!-<exp<c!$1(%:",M|
%|i"#Symbol^:#&c0  m:"&c0!*,K:#&c0  a}}:!&c0!*-<exp<c!$1(':",Mi|
%|<c!$1(#$^j^1_,M$^j^2_}}:#&c0  m:"&c0!*,M<c!$1(#$^j^1_,K$^j^2_}}|
%|:#&c0  a}}}}$^:"&c0!*h_^$^j^1_$^<c$%A^h}_^$^j^2_$^:#&c0  d_^$|
%|^:"&c0!*j^3_}(":!-<sum}()$^j^1_,K$^j^2_,K$^j^3_,]k,K1}_}: ;bP;/"|
%|:!;bP8 $^:"g^2_:#  :!,F:",]<c%#D($<2^$^G(!nm}_((1,M:!-<exp<c!$1|
%|($:"i:#&c0  m:"&c0!*,K:#&c0  a}}:!&c0!*-<exp<c!$1(':",Mi<c!$1|
%|(#$^j^1_,M$^j^2_}}:#&c0  m:"&c0!*,M<c!$1(#$^j^1_,K$^j^2_}}:#&c0  a}}}}|
%|$^:"&c0!*h_^$^j^1_$^<c$%A^h}_^$^j^2_$^:#&c0  d_^$^:"&c0!*j^3_}|
%|(":!-<sum}()$^j^1_,K$^j^2_,K$^j^3_,]k,K1}_} : -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {g}_{1}&=
\sum\limits_{{j}_{1}+{j}_{2}+{j}_{3}=k+1}^{} {{G}_{nm} \over 1-\exp\left({-i\mu +\alpha }\right)\exp\left({-i\left({{j}_{1}-{j}_{2}}\right)\mu -\left({{j}_{1}+{j}_{2}}\right)\alpha }\right)}{h}^{{j}_{1}}{\overline{h}}^{{j}_{2}}{\delta }^{{j}_{3}}\nonumber \\
 {g}_{2}  &=
\sum\limits_{{j}_{1}+{j}_{2}+{j}_{3}=k+1}^{} {{G}_{nm} \over 1-\exp\left({i\mu +\alpha }\right)\exp\left({-i\left({{j}_{1}-{j}_{2}}\right)\mu -\left({{j}_{1}+{j}_{2}}\right)\alpha }\right)}{h}^{{j}_{1}}{\overline{h}}^{{j}_{2}}{\delta }^{{j}_{3}} \end{align}
What follows is the {\tt subroutine analyse\_kernel} which performs the calculation order by order.
{
\footnotesize
\begin{verbatim}
  subroutine analyse_kernel(order,tune,alpha,nl,g,dtune_dA,dtune_dk)
    implicit none
    TYPE (my_map), INTENT (inout) :: nl
    type(vector_field), INTENT (inout) ::  g
    TYPE (my_map) t
    integer i,j,k,order,i1,i2
    type(vector_field) f
    real(dp) dtune_dA,dtune_dk,tune,alpha
    complex(dp) lam(2),denominator

    lam(1)=exp(-i_*tune*twopi+alpha)
    lam(2)=exp(i_*tune*twopi+alpha)
    
    g=0
    
     t=to_phasor*nl*from_phasor 
     f=t.vf.order
     
 do i=1,2
 do j=0,n_mono    
 if(jorder(j)/=order) cycle
  denominator= 1.0_dp-lam(i)*exp(i_*(jexp1(j)-jexp2(j))*tune*twopi-(jexp1(j)+jexp2(j))*alpha)
      
      if(epsresonance>0) then    !@1 &nbsp; determined by closeness to a resonance
          if(abs(denominator)>epsresonance) then
              g%v(i)%a(j) = f%v(i)%a(j)/denominator 
          endif 
      else             !@1 &nbsp; leaves tune shifts and amplitude dependent damping
          if((jexp1(j)-jexp2(j)-1/=0.and.i==1).or.(jexp1(j)-jexp2(j)+1/=0.and.i==2)) then
              g%v(i)%a(j) = f%v(i)%a(j)/denominator 
          endif
 endif      
 enddo
 enddo

  END subroutine analyse_kernel
\end{verbatim}
}

\section{\color{.} Canonization: {\tt \normalsize CANONIZE(Atot,A\_cs,disp,a\_l,a\_nl,R,PHASE\_ADVANCE)}}

This routine forces the map into a canonical form, that is to say, into a predetermined form. In the linear case, we choose:
%
%]|Expr|[#b @`b___})(# b'4" Chicago^: ;bP8&c0!*,^"!*|:!A,]<c!$1|
%|^["" ^$^A(!11}_^0^$^A(!12}_^$^A(!22}_}}: ;8/<:!;bP8cs: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}A=
\left({\begin{matrix}{A}_{11}&0\\
{A}_{12}&{A}_{22}\end{matrix}}\right)\label{cs}\end{eqnarray}
%
This is the so-called Courant-Snyder choice. There are legitimate physical reasons to prefer this choice over an arbitrary choice. Incidentally, this choice can be extended to the coupled case
by requiring $A_{34}=0$. In the toy package, we imposed this choice  to all orders in the parameter. 
 
The nonlinear part of the map is chosen (quite arbitrarily) to have no generator of rotation in its nonlinear operator. In this code, the nonlinear map is represented by a single Lie operator.

The end result of the code ``canonize'' is to rewrite {\tt Atot} as follows:

%
%]|Expr|[#b @`b___})*# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!A(!tot}|
%|_,]<c%"C^<cY A(%Disp""Symbol^:"&c0  .P$^:!&c0!*A^l_:"&c0  .P$|
%|^:!&c0!*A(!nl}_}}^$^A(!cs}__}:"&c0  .P:!&c0!*R,H:"&c0  j:!&c0!*,I|
%|: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{A}_{tot}=
\underbrace{Disp\circ {A}_{l}\circ {A}_{nl}}\limits_{{A}_{cs}}^{}\circ R(\varphi )\end{eqnarray}
%

$Disp$  brings the map to the fixed point to all orders. $A_l$ is the full linear map to all orders in the parameter. $A_{nl}$ is a pure nonlinear map. The map $R(\varphi )$ is a rotation where the angle $\varphi$ is a polynomial, i.e., an amplitude dependent angle. It is the phase advance if applied to a tracked canonical transformation. 

\section{\color{.}  Further explanations}\label{expl}
We expand \eq{maineq}:
%
%
%]|Expr|[#b @`b___})b!&# b'4" Chicago^: ;bP8&c0!*,^BN$^B_(""!*|:!,M|
%|1}""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" :#-<exp<c!$1(#:!g:"&c0  /7|
%|/1}}: &c0!*R$^N^"$Times|:$l_:#-<exp<c!$1($:!,Mg:"&c0  /7/1}}: ;bP;&c0!*/"|
%|:#;bP8 :" :#,F:!,]:#,F:" :#-<exp<c!$1(#:!g:"&c0  /7/1}}: &c0!*R|
%|:#-<exp<c!$1(#:!F:"&c0  /7/1}}:#&c0!*-<exp<c!$1($:!,Mg:"&c0  /7|
%|/1}}: ;bP;&c0!*/":#;bP8 :" :#,F:!,]:#,F:" : R:#-<exp<c!$1(%$^|
%|: R_(":!,M1}g:"&c0  /7/1: &c0!*R}}:#-<exp<c!$1(#:!F:"&c0  /7/1}}|
%|:#&c0!*-<exp<c!$1($:!,Mg:"&c0  /7/1}}: ;bP;&c0!*/":#;bP8 :" :#,F|
%|:!,]:#,F:" : R:#-<exp<c!$1(-$^: R_(":!,M1}g:"&c0  /7/1: &c0!*R|
%|,K:!F:"&c0  /7/1:!&c0!*,Mg:"&c0  /7/1}}"%Monaco^:%&c0!*,Khigher|
%| order: ;8/<:!;bP8maineq2: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{\cal B}{\cal N}{{\cal B}}^{-1} &=
& \exp\left({g\cdot \nabla }\right){\cal R}{{\cal N}}_{l}\exp\left({-g\cdot \nabla }\right)\nonumber \\
  &=
& \exp\left({g\cdot \nabla }\right){\cal R}\exp\left({F\cdot \nabla }\right)\exp\left({-g\cdot \nabla }\right)\nonumber \\
  &=
& {\cal R}\exp\left({{{\cal R}}^{-1}g\cdot \nabla {\cal R}}\right)\exp\left({F\cdot \nabla }\right)\exp\left({-g\cdot \nabla }\right)\nonumber \\
  &=
& {\cal R}\exp\left({{{\cal R}}^{-1}g\cdot \nabla {\cal R}+F\cdot \nabla -g\cdot \nabla }\right)+{\rm h}{\rm i}{\rm g}{\rm h}{\rm e}{\rm r}~{\rm o}{\rm r}{\rm d} {\rm e}{\rm r}\label{maineq2}\end{eqnarray}
%
\eq{maineq2} gives an equation on the Lie operators:
%
%]|Expr|[#b @`b___})7# b'4" Chicago^: ;bP8&c0!*,^$^R_(""!*|:!,M|
%|1}g""Symbol^:"&c0  /7/1: &c0!*R,K:!F:"&c0  /7/1:!&c0!*,Mg:"&c0  /7|
%|/1:!&c0!*,]$^F(!"#Monaco^:#kernel}_:"&c0  /7/1, : ;8&c0!*/<:!;bP8kernel|
%|: ;8/=:";bP8&c0  : &c0!*,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{{\cal R}}^{-1}g\cdot \nabla {\cal R}+F\cdot \nabla -g\cdot \nabla =
{F}_{{\rm k}{\rm e}{\rm r}{\rm n}{\rm e}{\rm l}}\cdot \nabla \label{kernel}\end{eqnarray}
%

${F}_{{\rm k}{\rm e}{\rm r}{\rm n}{\rm e}{\rm l}}$ is usually the generator of a rotation (hence the name ``kernel'') or may contain more objects such as the generators of a resonance.

Equations between operators are nasty to solve. In the symplectic case we transform them into equations between polynomials. Here our vector fields contain vector functions, namely $g$ and $F$ which are polynomials.
Thus we must transform \eq{kernel} into an equation between the vector functions. The answer is, even if $\cal R$ is nonlinear:
%
%]|Expr|[#b @`b___})?# b'4" Chicago^: ;bP8&c0!*,^$^R_(""!*|:!,M|
%|1}g""Symbol^:"&c0  /7/1: &c0!*R:" "#Helvetica|:#,F:!,]:#,F:" |
%|$^:!g_^t:"&c0  /7/1: ;bP;&c0!*/""$Monaco^:$;bP8 where $^:!g^k|
%|^t:#,F:!,]:#,F:" <c!$1($:!g:"&c0  /7/1$^:!&c0!*r^k_}}:"&c0  .P|
%|$^:!&c0!*r_(",M1}: ;8/<:!;bP8vectran: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{{\cal R}}^{-1}g\cdot \nabla {\cal R} &=
& {g}^{t}\cdot \nabla \nonumber \\
~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~{g}_{k}^{t}&=
& \left({g\cdot \nabla {r}_{k}}\right)\circ {r}^{-1}\label{vectran}\end{eqnarray}
%
In our case the map $r$ (or $\cal R$) is linear. We notice two terms: one rotates the vector and the other one transforms its functional dependence.

\begin{itemize}
\item  The first term represent the transformation of the vector itself. If the map $r$ is linear, it amounts to a linear transformation of the vector field. It is the term {\tt lam(i)} in the code since in the phasor's basis the map $r$ is diagonal.  
\item  The second term is the substitution  of $r^{-1}$ itself in the vector functions. This term can involve high order resonances if the original field is nonlinear. If the original field is linear, it also involves single powers of {\tt lam(i)}. Hence, as we know, linear matrices contain only second order resonances such as $2 {\mu }$ in the 1-d-o-f case.
\end{itemize}

In the phasors basis the map $r$ is totally diagonal. Indeed we have: 
%
%]|Expr|[#b @`b___})b B# b'4" Chicago^: ;bP8&c0!*,^$^R_(",M1}$|
%|^"!*|:!x^1_""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" $^:!x^1_:"&c0  .P|
%|$^:!&c0!*r_(",M1},]:#-<exp<c!$1(":!i:"&c0  m}}$^:!&c0!*x^1_: ;bP;/"|
%|:#;bP8 $^: R_(",M1}$^:!x^2_:" :#,F:!,]:#,F:" $^:!x^2_:"&c0  .P|
%|$^:!&c0!*r_(",M1},]:#-<exp<c!$1(#:!,Mi:"&c0  m}}$^:!&c0!*x^2_|
%|: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{{\cal R}}^{-1}{x}_{1} &=
& {x}_{1}\circ {r}^{-1}=
\exp\left({i\mu }\right){x}_{1}\nonumber \\
 {{\cal R}}^{-1}{x}_{2} &=
& {x}_{2}\circ {r}^{-1}=
\exp\left({-i\mu }\right){x}_{2}\end{eqnarray}
%
In the phasors basis, vector fields of the form
%
%]|Expr|[#b @`b___})%# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!x^1^$|
%|^j^1_$^x^2^$^j^2_$^""Symbol^:"&c0  .V^:!&c0!*a_: ,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{x}_{1}^{{j}_{1}}{x}_{2}^{{j}_{2}}{\partial }_{a}\end{eqnarray}
%
are eigenphasors. Indeed, as an example, let us apply \eq{vectran} on the vector field
%
%]|Expr|[#b @`b___})%# b'4" Helvetica|: ;bP8&c0!*,D$^"!*|:!x^1|
%|^4$^x^2^2$^""Symbol^:"&c0  .V^:!&c0!*1_: ,D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
${x}_{1}^{4}{x}_{2}^{2}{\partial }_{1}$.
%
%]|Expr|[#b @`b___})b O# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!F^1|
%|^t""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" <c!$1($$^:!x^1^4$^x^|
%|2^2$^:"&c0  .V^:!&c0!*1_$^r^1_}}:"&c0  .P$^:!&c0!*r_(",M1}: ;bP;/"|
%|:#;bP8 :" :#,F:!,]:#,F:" <c!$1(#$^:!x^1^4$^x^2^2$^e_(#,Mi:"&c0  m}}}|
%|.P$^:!&c0!*r_(",M1}: ;bP;/":#;bP8 :" :#,F:!,]:#,F:" :#$^:!e_(#|
%|i4:"&c0  m}$^:!&c0!*x^1^4$^e_($,Mi2:"&c0  m}$^:!&c0!*x^2^2$^e|
%|_(#,Mi:"&c0  m}:!&c0!*,]$^e_("i:"&c0  m}$^:!&c0!*x^1^4$^x^2^2|
%|: ;bP;/":#;bP8 :" :#,F:!,]:#,F:" $^:!e_("i:"&c0  m}$^:!&c0!*F|
%|^1^t: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{F}_{1}^{t} &=
& \left({{x}_{1}^{4}{x}_{2}^{2}{\partial }_{1}{r}_{1}}\right)\circ {r}^{-1}\nonumber \\
  &=
& \left({{x}_{1}^{4}{x}_{2}^{2}{e}^{-i\mu }}\right)\circ {r}^{-1}\nonumber \\
  &=
& {e}^{i4\mu }{x}_{1}^{4}{e}^{-i2\mu }{x}_{2}^{2}{e}^{-i\mu }=
{e}^{i\mu }{x}_{1}^{4}{x}_{2}^{2}\nonumber \\
  &=
& {e}^{i\mu }{F}_{1}^{t}\end{eqnarray}
%
For that eigenvector field, the solution for $g$ is: 
%
%]|Expr|[#b @`b___})(# b'4" Chicago^: ;bP8&c0!*,^"!*|:!g""Symbol^:"&c0  /7|
%|/1:!&c0!*,]<2)"# b'4$^x^1^4$^x^2^2}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|)## b'41,M$^e_("i:"&c0  m}}& b!( b"0 b#8 b$@ b%H b&P!WW}}<2^.V|
%|(".V$^:!&c0!*x^1_}}: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}g\cdot \nabla =
{{x}_{1}^{4}{x}_{2}^{2} \over 1-{e}^{i\mu }}{\partial  \over \partial {x}_{1}}\end{eqnarray}
%
I leave it to the reader to do a dilation. In that case, the  eigenvalues of the phasors are multiplied by a real factor of the form $\exp (-\alpha )$. Everything else follows as before.



\chapter{\color{.} Hierarchy of Analytical Methods}\label{chapgf}


In this chapter, I will perform a simple {\bf first order} nonlinear calculation on the Hamiltonian of the  simple code/package in directory \htmte{build_my_own_tiny_package}. The calculation will be performed by two different analytical methods and will reproduce the results of FPP of \sec{secdispgen}. We will show that the Taylor method of the codes matches the Green's function method (map method actually). Therefore, in our way of thinking, methods based on Fourier transforms  are further down the hierarchy. They are further away from the tracking code.


Our simple example will be that of the tracking code with a sextupole, i.e., \sec{secdispgen}. The Hamiltonian of our codes looks like this:
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"H:!,F,]<c%"C^<cY A('<2^$^:"p_^2^2},K$^k^Q_,Hs,I<2^$^x_^|
%|2^2}}}^$^H^0__},K$^k^S_,Hs,I<2^$^x_^3^6}: ;bP;/";8/<:";bP8hex|
%|: ;8/=:!;bP8 : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} H&=\underbrace{{{p}^{2} \over 2}+{k}_{Q}(s){{x}^{2} \over 2}}\limits_{{H}_{0}}^{}+{k}_{S}(s){{x}^{3} \over 6}\nonumber \\
\label{hex} \end{align}
%
%
This definition of the sextupole strength matches the tiny package Hamiltonian (\htmt{y_my_own_small_code.f90}) and the code MAD of CERN. For PTC, just replace ``6'' by ``3'' in \eq{hex}.

The particular result we will  derive is the average of the position, i.e.,
$\left\langle{{x}}\right\rangle$ as in \sec{secdispgen}, \eq{xana}.


\section{\color{.}Green's function Method}\label{secgf}

In reference \cite{thebook}, the author, yours truly,  lists the rules of analytical perturbation theory as they apply to a map based methods. What if we introduce a certain potential around the ring
and  want to evaluate a quantity to order ${\kappa }$ in that potential? The following rules must be followed in the map based method.


\subsection{\color{.}The rules of Analytical perturbation theory with maps}\label{secgreenrule}
\v5
\v5
{
\noindent \bf Rules for ${\kappa }^{\rm th}$ order analytical perturbation theory on maps.

\noindent (Green's methods really...)
} 
\begin{enumerate} 
\item\label{rule1} Go into the Floquet variables of the problem you already know how to solve: all the unperturbed maps should be rotations.
\item\label{rule2} Transform  the perturbations  by the canonical transformation which gave you the rotations of item \ref{rule1}.
\item\label{rule3} Insert ${\kappa }$ distinct perturbations in the ring labelled  from one to ${\kappa }$ in order of appearance. The one-turn map at some arbitrary point will
be made of a product of  $2*{\kappa }+1$ maps. 
\item\label{rule4} Compute the one-turn map by ``phase advancing''  all the perturbations at the beginning or end of the ring  
using the zeroth order propagator of item \ref{rule1}; generally rotations.
\item\label{rule5} Lump all the perturbations  into one exponent  using the BCH\index{BCH} theorem. 
\item\label{rule6} Compute the quantity of interest, the average of $x$ for example.
\item\label{rule7} Sum/integrate over the actual distribution of the perturbations using the index $j$ labeling the perturbations ($1\ge j \le \kappa$) as a 
time-ordering label.   
\end{enumerate}

\v5
\subsection{\color{.}The Actual Calculation with Maps}\label{secgreencalc}

Let us apply each one of these rules on the Hamiltonian of \eq{hex}.
First, we assume that the linear one-turn map generated by $H_0$ can be normalised  at any position  $s$. So the relevant  matrices obey 
%
%]|Expr|[#b @`b___})/# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"M^s_:!,F,]$^A^s_R, $^A^s(",M1} : ;8/<:!;bP8nf1d: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {M}_{s}&={A}_{s}R{A}_{s}^{-1} \label{nf1d}\end{align}
For $A_s$ we can choose the Courant-Snyder definition:
%
%]|Expr|[#b @`b___})*# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^A^s_,F,]<c!$1^["" ^<b R^$^""Symbol^:"&c0  b^"#*|:#&c0!*s__}|
%|^:!0(":#,M<2^$^:"&c0  a^:#&c0!*s_^<b R^$^:"&c0  b^:#&c0!*s__}}}|
%|^<2^:!1^<b R^$^:"&c0  b^:#&c0!*s__}}}}:! : ;8/<:!;bP8css: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {A}_{s}&=\left({\begin{matrix}\sqrt {{\beta }_{s}}&0\\
-{{\alpha }_{s} \over \sqrt {{\beta }_{s}}}&{1 \over \sqrt {{\beta }_{s}}}\end{matrix}}\right) \label{css}\end{align}
We know from general principles, that if we apply \eq{css} to \eq{hex}, the result will be:
%
%]|Expr|[#b @`b___})2# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"H:!,F,]$^"#Symbol^:#&c0  w^:"&c0!*s_J,K$^:#&c0  b^:"&c0!*s|
%|(#3,O2}$^k^S_,Hs,I<2^$^x_^3^6}: ;8/<:";bP8hexf: ;8/=:!;bP8 : -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} H&={\omega }_{s}J+{\beta }_{s}^{3/2}{k}_{S}(s){{x}^{3} \over 6}\label{hexf} \end{align}
The form of ${\omega }_{s}$ is not important in an actual calculation since the phase advances will be computed by the code. Of course for \eq{hex} with $A_s$, as defined in \eq{css}, the answer is actually 
%
%]|Expr|[#b @`b___})(# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  w^"#*|:#&c0!*s_:!,F,] <2^1^$^:"&c0  b^:#&c0!*s|
%|_},N: -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\omega }_{s}&= {1 \over {\beta }_{s}}.\end{align}

Next we apply rule \ref{rule2} to compute the map of a single pertubation at some point $s$. The answer, in terms of Lie maps, is just:
%
%]|Expr|[#b @`b___})+# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^: P^""*|:"s_:!,F,]-<exp<c!$1)*# b'4:",Mds,Z$^"#Symbol^:#&c0  b|
%|^:"&c0!*s(#3,O2}$^k^S_,Hs,I<2^$^x_^3^6},Z|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}:! : ;8/<:";bP8perts: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {{\cal P}}_{s}&=\exp\left({-ds:{\beta }_{s}^{3/2}{k}_{S}(s){{x}^{3} \over 6}:}\right) \label{perts}\end{align}
Now we construct the map according to rule \ref{rule3}. Since we are doing a first order calculation, we need only one map ${\cal P}_{s} $ in our ``abstract'' ring. 
The  one-turn map at some position $s$ is given by:
%
%]|Expr|[#b @`b___})-# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^M^s_,F,]$^: R(":!s$^s^1_}_$^: P^$^:!s^1__$^: R("$^:!s^1_s}_|
%| : ;8/<""*|:";bP8perts1: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {M}_{s}&={{\cal R}}_{s{s}_{1}}{{\cal P}}_{{s}_{1}}{{\cal R}}_{{s}_{1}s} \label{perts1}\end{align}
\fbox{
\vbox{  {\large \bf  Remark}: If we were interested in a second order calculation, then the equivalent of \eq{perts1} would be:  
\v5
%
%]|Expr|[#b @`b___})/# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^M^s_,F,]$^: R(":!s$^s^1_}_$^: P^$^:!s^1__$^: R("$^:!s^1_$^s|
%|^2_}_$^: P^$^:!s^2__$^: R("$^:!s^2_s}_ : ;8/<""*|:";bP8perts2|
%|: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {M}_{s}&={{\cal R}}_{s{s}_{1}}{{\cal P}}_{{s}_{1}}{{\cal R}}_{{s}_{1}{s}_{2}}{{\cal P}}_{{s}_{2}}{{\cal R}}_{{s}_{2}s} \label{perts2}\end{align}
For example, this is necessary in the calculation of the tune shift due to a sextupole distribution. Here we stick to a simple, first order, albeit nonlinear calculation.
}
}
\v5
Now we phase advance the perturbation in \eq{perts1} as demanded by rule \ref{rule4}:
%
%]|Expr|[#b @`b___})!# b'4["!!) # b'4}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|)b L# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! $^M^s_,F,]$^: R|
%|(":!s$^s^1_}_$^: P^$^:!s^1__$^: R("$^:!s^1_s}_: ;bP;/":!;bP8 |
%|,F,]$^: R(":!s$^s^1_}_$^: P^$^:!s^1__$^: R(":!s$^s^1_}(",M1}<c%"C|
%|^<cY A("$^: R(":!s$^s^1_}_$^: R("$^:!s^1_s}_}}^: R_}:! : ;bP;/"|
%|:!;bP8 ,F,] -<exp<c!$1),# b'4""*|:",Mds$^: R(":!s$^s^1_}_:",Z|
%|$^"#Symbol^:#&c0  b^$^:!&c0!*s^1_(#:"3,O2}$^k^S_,H$^:!s^1_:",I|
%|<2^$^x_^3^6},Z$^: R(":!s$^s^1_}(",M1}}& b!( b"0 b#8 b$@ b%H b&P!WW}}|
%|: R ;bP;/":!;bP8 ,F,] -<exp<c!$1)*# b'4:",Mds,Z$^:#&c0  b^:"&c0!*s|
%|(#3,O2}$^k^S_,H$^:!s^1_:",I<2)"# b'4$^: R(":!s$^s^1_}_$^:"x_^|
%|3}& b!( b"0 b#8 b$@ b%H b&P!WW}^6},Z}& b!( b"0 b#8 b$@ b%H b&P!WW}}|
%|: R;bP;/":!;bP8 ,F,] -<exp<c!$1)*# b'4:",Mds,Z$^:#&c0  b^$^:!&c0!*s|
%|^1_(#:"3,O2}$^k^S_,H$^:!s^1_:",I<2)!# b'4$)!# b'4<c!$1("$^: R|
%|(":!s$^s^1_}_:"x}}}& b!( b"0 b#8 b$@ b%H b&P!WW}_^3|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^6},Z}& b!( b"0 b#8 b$@ b%H b&P!WW}}|
%|: R;8/<:";bP8pertsph: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}! !! !                  }|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[

\begin{align} {M}_{s}&={{\cal R}}_{s{s}_{1}}{{\cal P}}_{{s}_{1}}{{\cal R}}_{{s}_{1}s}\nonumber \\
 &={{\cal R}}_{s{s}_{1}}{{\cal P}}_{{s}_{1}}{{\cal R}}_{s{s}_{1}}^{-1}\underbrace{{{\cal R}}_{s{s}_{1}}{{\cal R}}_{{s}_{1}s}}\limits_{{\cal R}}^{} \nonumber \\
 &= \exp\left({-ds{{\cal R}}_{s{s}_{1}}:{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){{x}^{3} \over 6}:{{\cal R}}_{s{s}_{1}}^{-1}}\right){\cal R} \nonumber \\
 &= \exp\left({-ds:{\beta }_{s}^{3/2}{k}_{S}({s}_{1}){{{\cal R}}_{s{s}_{1}}{x}^{3} \over 6}:}\right){\cal R}\nonumber \\
 &= \exp\left({-ds:{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){{\left({{{\cal R}}_{s{s}_{1}}x}\right)}^{3} \over 6}:}\right){\cal R}\label{pertsph}\end{align}
\fbox{
\vbox{  {\large \bf  OK!}: Rule \ref{rule5} is not relevant to a first order calculation because \eq{pertsph} has only one factor containing the perturbation.
}
}
\v5

We are now on the most complex step. We need to compute the ``quantity of interest'' which in our case is the average of the position $x$. To do this, we need  to perform a nonlinear  canonical transformation which depends on the perturbation. The purpose of this transformation is to turn the perturbed map into a rotation to leading order in the perturbation.  We can then perform the average to leading order, i.e., to first order in sextupole strength.
%
%]|Expr|[#b @`b___})b J# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^: R^1_:! ,F,] -<exp<c!$1(#""*|:",ZF,Z}}:!-<exp<c!$1)*# b'4:",M|
%|ds,Z$^"#Symbol^:#&c0  b^$^:!&c0!*s^1_(#:"3,O2}$^k^S_,H$^:!s^1|
%|_:",I<2)!# b'4$)!# b'4<c!$1("$^: R(":!s$^s^1_}_:"x}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_^3}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|^6},Z}& b!( b"0 b#8 b$@ b%H b&P!WW}}: R:!-<exp<c!$1($:",Z,MF,Z}}|
%|: ;bP;/":!;bP8 ,F,] -<exp<c!$1(#:",ZF,Z}}:!-<exp<c!$1)*# b'4:",M|
%|ds,Z$^:#&c0  b^$^:!&c0!*s^1_(#:"3,O2}$^k^S_,H$^:!s^1_:",I<2)!# b'4|
%|$)!# b'4<c!$1("$^: R(":!s$^s^1_}_:"x}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_^3}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|^6},Z}& b!( b"0 b#8 b$@ b%H b&P!WW}}:!-<exp<c!$1($:",Z,M: R:"F|
%|,Z}}: R;bP;/":!;bP8 ,F,]-<exp<c!$1)/# b'4:",ZF,M: RF:",Mds$^:#&c0  b|
%|^$^:!&c0!*s^1_(#:"3,O2}$^k^S_,H$^:!s^1_:",I<2)!# b'4$)!# b'4<c!$1|
%|("$^: R(":!s$^s^1_}_:"x}}}& b!( b"0 b#8 b$@ b%H b&P!WW}_^3|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^6},K'b M,Z|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}: R;8/<:";bP8pertsph1: ;8/=:!;bP8 |
%|: -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {{\cal R}}_{1} &= \exp\left({:F:}\right)\exp\left({-ds:{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){{\left({{{\cal R}}_{s{s}_{1}}x}\right)}^{3} \over 6}:}\right){\cal R}\exp\left({:-F:}\right)\nonumber \\
 &= \exp\left({:F:}\right)\exp\left({-ds:{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){{\left({{{\cal R}}_{s{s}_{1}}x}\right)}^{3} \over 6}:}\right)\exp\left({:-{\cal R}F:}\right){\cal R}\nonumber \\
 &=\exp\left({:F-{\cal R}{\cal F}-ds{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){{\left({{{\cal R}}_{s{s}_{1}}x}\right)}^{3} \over 6}+\cdots:}\right){\cal R}\label{pertsph1} \end{align}
We are left with the following possibility:
%
%]|Expr|[#b @`b___});# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!$1(#""*|:"I,M: R}}:"F,Mds$^"#Symbol^:#&c0  b^$^:!&c0!*s^1_|
%|(#:"3,O2}$^k^S_,H$^:!s^1_:",I<2)!# b'4$)!# b'4<c!$1("$^: R(":!s|
%|$^s^1_}_:"x}}}& b!( b"0 b#8 b$@ b%H b&P!WW}_^3|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^6}:# :!,F-<stackrel-[:",_:!-]-[|
%|:",]:!-]:"0:! : ;8/<:";bP8ker0: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left({I-{\cal R}}\right)F-ds{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){{\left({{{\cal R}}_{s{s}_{1}}x}\right)}^{3} \over 6} &\stackrel{?}{=
}0 \label{ker0}\end{align}
Can we choose $F$ so that \eq{ker0} is satisfied? In general we can chose $F$ so as to leave only powers of the action $J$ in the exponent. These produce tune shifts. 
However, as is well known about sextupoles, they produce no amplitude dependent tune shifts in leading order. Thus \eq{ker0} can be satisfied by a proper selection of $F$.
If we express $x^3$ in the eigenphasors basis of \eq{phasors}, the above statements will be clear:
%
%]|Expr|[#b @`b___})b N# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$)!# b'4<c!$1("$^: R(":!s$^s^1_}_""*|:"x}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_^3:!,F,]<2^1^8}$^<c!$1(%$^: R("|
%|:!s$^s^1_}_$^"#Symbol^:#&c0  F^:"&c0!*,K_,K$^: R(":!s$^s^1_}_|
%|$^:#&c0  F^:"&c0!*,M_}}_^3: ;bP;/":!;bP8 ,F,]<2^1^8}$^<c!$1(%|
%|$^:"e_(#,Mi:#&c0  D}$^F^:"&c0!*,K_,K$^e_("i:#&c0  D}$^F^:"&c0!*,M|
%|_}}_^3: ;bP;/":!;bP8 ,F,]<2^1^8}$^<c!$1(/$^:"e_($,Mi3:#&c0  D}|
%|$^F^:"&c0!*,K^3,K$^e_(#i3:#&c0  D}$^F^:"&c0!*,M^3,K3$^e_(#,Mi|
%|:#&c0  D}$^F^:"&c0!*,K^2$^:#&c0  F^:"&c0!*,M_,K3$^e_(#,Mi:#&c0  D}|
%|$^F^:"&c0!*,K_$^:#&c0  F^:"&c0!*,M^2}}__: ;8/<:";bP8rx3: ;8/=|
%|:#;bP8 :!-<-<:# "$Monaco^:$where :#&c0  D:!&c0!*,F,]$^:#&c0  m|
%|(":"&c0!*s$^s^1_}_:$  :#&c0  .L:"&c0!*  :$phase advance from |
%|:"s:$ to $^:"s^1_:! : .O-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\left({{{\cal R}}_{s{s}_{1}}x}\right)}^{3}&={1 \over 8}{\left({{{\cal R}}_{s{s}_{1}}{\Phi }_{+}+{{\cal R}}_{s{s}_{1}}{\Phi }_{-}}\right)}^{3}\nonumber \\
 &={1 \over 8}{\left({{e}^{-i\Delta }{\Phi }_{+}+{e}^{i\Delta }{\Phi }_{-}}\right)}^{3}\nonumber \\
 &={1 \over 8}{\left({{e}^{-i3\Delta }{\Phi }_{+}^{3}+{e}^{i3\Delta }{\Phi }_{-}^{3}+3{e}^{-i\Delta }{\Phi }_{+}^{2}{\Phi }_{-}+3{e}^{-i\Delta }{\Phi }_{+}{\Phi }_{-}^{2}}\right)}^{}\label{rx3} \\ {\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~\Delta &={\mu }_{s{s}_{1}}~~\leftarrow \ \ {\rm p}{\rm h}{\rm a}{\rm s}{\rm e}~{\rm a}{\rm d} {\rm v}{\rm a}{\rm n}{\rm c}{\rm e}~{\rm f}{\rm r}{\rm o}{\rm m}~s~{\rm t}{\rm o}~{s}_{1} \nonumber 
\end{align}
The answer for $F$ us thus:%
%
%]|Expr|[#b @`b___})9# b'4" Chicago^: ;bP8&c0!*-="!Times|:!F""Helvetica|:",F,]|
%|"#*|:#ds$^"$Symbol^:$&c0  b^$^:"&c0!*s^1_(#:#3,O2}$^k^S_,H$^:"s|
%|^1_:#,I$^<c!$1(#I,M: R}}_(":#,M1}<2)!# b'4$)!# b'4<c!$1("$^: R|
%|(":"s$^s^1_}_:#x}}}& b!( b"0 b#8 b$@ b%H b&P!WW}_^3|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^6}: ;bP;/":";bP8,F,]<2)!# b'4:#ds|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(!:"48}}$^:$&c0  b^$^:"&c0!*s^1|
%|_(#:#3,O2}$^k^S_,H$^:"s^1_:#,I<c!$1(-<2^$^e_($,Mi3:$&c0  D}(#|
%|:#&c0!*1,M$^e_($,Mi3:$&c0  m}}}$^F^:#&c0!*,K^3,K<2^$^e_(#i3:$&c0  D}|
%|(#:#&c0!*1,M$^e_(#i3:$&c0  m}}}$^F^:#&c0!*,M^3,K<2("3$^e_(#,M|
%|i:$&c0  D}}(#:#&c0!*1,M$^e_(#,Mi:$&c0  m}}}$^F^:#&c0!*,K^2$^:$&c0  F|
%|^:#&c0!*,M_,K<2("3$^e_("i:$&c0  D}}(#:#&c0!*1,M$^e_("i:$&c0  m}}}|
%|$^F^:#&c0!*,K_$^:$&c0  F^:#&c0!*,M^2}}:" : ;8/<:#;bP8fans: ;8/=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align}F&=ds{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){\left({I-{\cal R}}\right)}^{-1}{{\left({{{\cal R}}_{s{s}_{1}}x}\right)}^{3} \over 6}\nonumber \\
&={ds \over 48}{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1})\left({{{e}^{-i3\Delta } \over 1-{e}^{-i3\mu }}{\Phi }_{+}^{3}+{{e}^{i3\Delta } \over 1-{e}^{i3\mu }}{\Phi }_{-}^{3}+{3{e}^{-i\Delta } \over 1-{e}^{-i\mu }}{\Phi }_{+}^{2}{\Phi }_{-}+{3{e}^{i\Delta } \over 1-{e}^{i\mu }}{\Phi }_{+}{\Phi }_{-}^{2}}\right) \label{fans}\end{align}
We can now find the average of $x$ by transforming $x$ with the full canonical transformation:
%
%]|Expr|[#b @`b___})b D# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c$4Q^""*|:"x}:!,F,]-<exp<c!$1(#:",ZF,Z}}$^: A^:"s_x: ;bP;/":!;bP8 |
%|,F,]<c!$1(&1,K:",ZF,Z,K'b M}}: ;bP;/":!;bP8,F,]<b R^$^"#Symbol^:#&c0  b|
%|^:"&c0!*s__}x,K<b R^$^:#&c0  b^:"&c0!*s__}<c!=Q(#F,Lx}},K'b M|
%|:! : ;bP;/":!;bP8 ,F,]<b R^$^:#&c0  b^:"&c0!*s__}<c!$1)## b'4|
%|<2(#$^:#&c0  F^:"&c0!*,K_,K$^:#&c0  F^:"&c0!*,M_}^2},K<2(#<c!=Q|
%|(#F,L$^:#&c0  F^:"&c0!*,K_}},K<c!=Q(#F,L$^:#&c0  F^:"&c0!*,M_}}}|
%|^2}}& b!( b"0 b#8 b$@ b%H b&P!WW}}: ;bP;/":!;bP8 ,F,]<b R^$^:#&c0  b|
%|^:"&c0!*s__}<c!$1)$# b'4<2(#$^:#&c0  F^:"&c0!*,K_,K$^:#&c0  F|
%|^:"&c0!*,M_}^2},Ki<c!(A(#<2(":#&c0  .V:"&c0!*F}(#:#&c0  .V$^F|
%|^:"&c0!*,M_}},M<2(":#&c0  .V:"&c0!*F}(":#&c0  .V$^F^:"&c0!*,K|
%|_}}}}}& b!( b"0 b#8 b$@ b%H b&P!WW}}: ;8/<:";bP8pbx: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \widetilde{x}&=\exp\left({:F:}\right){{\cal A}}_{s}x\nonumber \\
 &=\left({1+:F:+\cdots}\right)\nonumber \\
&=\sqrt {{\beta }_{s}}x+\sqrt {{\beta }_{s}}\left[{F,x}\right]+\cdots \nonumber \\
 &=\sqrt {{\beta }_{s}}\left({{{\Phi }_{+}+{\Phi }_{-} \over 2}+{\left[{F,{\Phi }_{+}}\right]+\left[{F,{\Phi }_{-}}\right] \over 2}}\right)\nonumber \\
 &=\sqrt {{\beta }_{s}}\left({{{\Phi }_{+}+{\Phi }_{-} \over 2}+i\left\{{{\partial F \over \partial {\Phi }_{-}}-{\partial F \over \partial {\Phi }_{+}}}\right\}}\right)\label{pbx}\end{align}
Because the phasors in \eq{pbx} move on circles, we can average $\widetilde{x} $ immediately (see \eq{fraved}):
%
%]|Expr|[#b @`b___})b T# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<c!,Q^<c$4Q^""*|:"x}}:! ,F,]i<b R^$^"#Symbol^:#&c0  b^:"&c0!*s|
%|__}<c!,Q(#<2(":#&c0  .V:"&c0!*F}(":#&c0  .V$^F^:"&c0!*,M_}},M|
%|<2(":#&c0  .V:"&c0!*F}(":#&c0  .V$^F^:"&c0!*,K_}}}}: ;bP;/":!;bP8 |
%|,F,]<b R^$^:#&c0  b^:"&c0!*s__}2 :!-<Im<c!,Q^<2(":#&c0  .V:"&c0!*F}|
%|(":#&c0  .V$^F^:"&c0!*,K_}}}: ;bP;/":!;bP8  ,F,]<2)!# b'4:"ds|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^:!4}$^:#&c0  b^:"&c0!*s_$^:#&c0  b|
%|^$^:!&c0!*s^1_(#:"3,O2}$^k^S_,H$^:!s^1_:",I$^:#&c0  F^:"&c0!*,K|
%|_$^:#&c0  F^:"&c0!*,M_ :!-<Im<c!$1^<2^$^:"e_(#,Mi:#&c0  D}(#:"&c0!*1|
%|,M$^e_(#,Mi:#&c0  m}}}}: ;bP;&c0!*/":!;bP8 ,F,]<2)!# b'4:"ds|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^:!4}$^:#&c0  b^:"&c0!*s(#1,O2}|
%|$^:#&c0  b^$^:!&c0!*s^1_(#:"3,O2}$^k^S_,H$^:!s^1_:",I<2((,M:!-<|
%|sin<c!$1^:#&c0  D}:"&c0!*,K:!-<sin<c!$1(#:#&c0  D:"&c0!*,M:#&c0  m}}}|
%|(%:"&c0!*1,M:!-<cos<c!$1^:#&c0  m}}}:"&c0!* J : ;8/<:!pbxa: /=|
%|;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \left\langle{\widetilde{x}}\right\rangle &=i\sqrt {{\beta }_{s}}\left\langle{{\partial F \over \partial {\Phi }_{-}}-{\partial F \over \partial {\Phi }_{+}}}\right\rangle\nonumber \\
 &=\sqrt {{\beta }_{s}}2\ \Im\left\langle{{\partial F \over \partial {\Phi }_{+}}}\right\rangle\nonumber \\
  &={ds \over 4}{\beta }_{s}{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){\Phi }_{+}{\Phi }_{-}\ \Im\left({{{e}^{-i\Delta } \over 1-{e}^{-i\mu }}}\right)\nonumber \\
 &={ds \over 4}{\beta }_{s}^{1/2}{\beta }_{{s}_{1}}^{3/2}{k}_{S}({s}_{1}){-\sin\left({\Delta }\right)+\sin\left({\Delta -\mu }\right) \over 1-\cos\left({\mu }\right)}\ J\ \label{pbxa}\end{align}
Finally we apply rule \ref{rule7}. We sum/integrate over the entire ring. 
%
%]|Expr|[#b @`b___})+# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<2("""Symbol^:"&c0  .V<c!,Q^<c$4Q^"#*|:#&c0!*x}}}(":"&c0  .V:#&c0!*J}}|
%|:!,F,]<2)!# b'4$^:"&c0  b^:#&c0!*s(#1,O2}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}("4<c!$1(%1,M:!-<cos<c!$1^:"&c0  m}}}}}|
%|<c"$#((<c!$1((:#&c0!*,M:!-<sin<c!$1^$^:"&c0  m(":#&c0!*s:"&c0  s}|
%|_}:#&c0!*,K:!-<sin<c!$1(#$^:"&c0  m(":#&c0!*s:"&c0  s}_:#&c0!*,M|
%|:"&c0  m}}}}$^b^s(#:#&c0!*3,O2}$^k^S_,H:"&c0  s:#&c0!*,Id:"&c0  s}|
%|^:#&c0!*0^C}: ;8/<:#;bP8avesum: ;8/=:!;bP8 : -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\partial \left\langle{\widetilde{x}}\right\rangle \over \partial J}&={{\beta }_{s}^{1/2} \over 4\left({1-\cos\left({\mu }\right)}\right)}\oint_{0}^{C}\left({-\sin\left({{\mu }_{s\sigma }}\right)+\sin\left({{\mu }_{s\sigma }-\mu }\right)}\right){\beta }_{\sigma }^{3/2}{k}_{S}(\sigma )d\sigma \label{avesum} \end{align}
{\bf Notice:  we could have applied rule \ref{rule7} first to the polynomial $F$. Indeed $F$ is given by: 
%
%]|Expr|[#b @`b___}))# b'4" Chicago^: ;bP8&c0!*-="!Times|:!F""Helvetica|:",F,]|
%|<c"$#('<2)"# b'4"#*|:#d"$Symbol^:$&c0  s|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(!:"&c0!*48}}$^:$&c0  b^s(#:#&c0!*3|
%|,O2}$^k^S_,H:$&c0  s:#&c0!*,I<c!$1(-<2^$^e_($,Mi3$^:$&c0  m("|
%|:#&c0!*s:$&c0  s}_}(#:#&c0!*1,M$^e_($,Mi3:$&c0  m}}}$^F^:#&c0!*,K|
%|^3,K<2^$^e_(#i3$^:$&c0  m(":#&c0!*s:$&c0  s}_}(#:#&c0!*1,M$^e|
%|_(#i3:$&c0  m}}}$^F^:#&c0!*,M^3,K<2("3$^e_(#,Mi$^:$&c0  m(":#&c0!*s|
%|:$&c0  s}_}}(#:#&c0!*1,M$^e_(#,Mi:$&c0  m}}}$^F^:#&c0!*,K^2$^|
%|:$&c0  F^:#&c0!*,M_,K<2("3$^e_("i$^:$&c0  m(":#&c0!*s:$&c0  s}|
%|_}}(#:#&c0!*1,M$^e_("i:$&c0  m}}}$^F^:#&c0!*,K_$^:$&c0  F^:#&c0!*,M|
%|^2}}}^0^C}:" : ;8/<:#;bP8fanst: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align}F&=\oint_{0}^{C}{d\sigma  \over 48}{\beta }_{\sigma }^{3/2}{k}_{S}(\sigma )\left({{{e}^{-i3{\mu }_{s\sigma }} \over 1-{e}^{-i3\mu }}{\Phi }_{+}^{3}+{{e}^{i3{\mu }_{s\sigma }} \over 1-{e}^{i3\mu }}{\Phi }_{-}^{3}+{3{e}^{-i{\mu }_{s\sigma }} \over 1-{e}^{-i\mu }}{\Phi }_{+}^{2}{\Phi }_{-}+{3{e}^{i{\mu }_{s\sigma }} \over 1-{e}^{i\mu }}{\Phi }_{+}{\Phi }_{-}^{2}}\right) \label{fanst}\end{align}
}
\section{ \color{.} Calculations with the Hamiltonian }\label{sechamc}

We will describe how calculations are done on the Hamiltonian using a Fourier transform in  the time-like position $s$. This is the traditional approach in accelerator physics as championed
by Guignard and others. It is, of course, in S.Y. Lee's book\cite{sybook}.
 

\subsection{ \color{.} Changing the time-like variable into a phase advance }\label{subsechantime}


It is best to change the ``time''  variable $s$ into the actual phase advance of the linear part of $H$. 
%
%]|Expr|[#b @`b___})b S# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"H:!,F,]$^"#Symbol^:#&c0  w^:"&c0!*s_J,K$^:#&c0  b^:"&c0!*s|
%|(#3,O2}$^k^S_,Hs,I<2^$^x_^3^6}: ;bP;/":!;bP8 :# :!,F:#&c0  /?|
%|:!&c0!*,F:# : ;bP;/":!;bP8 "$Times|:$K:!,F,]:#&c0  n:"&c0!*J,K|
%|<2^:#&c0  n^$^w^:"&c0!*s_}$^:#&c0  b^q(#:"&c0!*3,O2}$^k^S_,H:#&c0  q|
%|:"&c0!*,I<2^$^x_^3^6}:! : ;8/<:";bP8hexft: ;8/=:!;bP8 :# :!-<-<|
%|:# "%Monaco^:%where :#&c0  qn:!&c0!*,F,]<c" #(#$^:#&c0  w^s_:"&c0!*d|
%|:#&c0  s}^:!&c0!*0^s}: ;8/<:!time: /=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} H&={\omega }_{s}J+{\beta }_{s}^{3/2}{k}_{S}(s){{x}^{3} \over 6}\nonumber \\
  &\Downarrow & \nonumber \\
 K&=\nu J+{\nu  \over {\omega }_{s}}{\beta }_{\theta }^{3/2}{k}_{S}(\theta ){{x}^{3} \over 6} \label{hexft}  \\ {\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~\theta \nu &=\int_{0}^{s}{\omega }_{\sigma }d\sigma \label{time}\end{align}
\v5
\noindent\fbox{
\vbox{  {\large \bf  2-d-f or more}: 
In several degrees of freedom, the trick of \eq{time} does not work alone. In order to get a form like say,
%
%]|Expr|[#b @`b___})-# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""Times|:"K:!,F,]$^"#Symbol^:#&c0  n^"$*|:$&c0!*1_$^J^1_,K$^:#&c0  n|
%|^:$&c0!*2_$^J^2_,KV,L: .O-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} K&={\nu }_{1}{J}_{1}+{\nu }_{2}{J}_{2}+V,\nonumber 
\end{align}
the other phases must be modified. This amounts  to a change of the phase advances, or equivalently, a change of the linear matrix $A_s$. The perturbation receives  extra phases so that in the end
everything  still applies as described here although   we confine ourselves to the 1-d-f case for simplicity. 
}
}
\v5
\subsection{\color{.} The Fourier method approach: Guignard}\label{secgui}

The $s$ or $\theta $ dependence of  \eq{hexft} is quite arbitrary. In accelerator physics, we can assume very little about the linear and the nonlinear parts besides a general Liouville-Arnorld structure near the origin, i.e., the motion sits on deformed tori or doughnuts. Faced with this conundrum, we are forced to expand the perturbation into some set of known functions. The standard approach of accelerator physicists, which is far from the code and often far from ideal, is to expand \eq{hexft} in a Fourier series in the periodic variable $\theta $.
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*.["!Helvetica|:! |
%|<2(&""Symbol^:"&c0  n$^b^q(#"#*|:#&c0!*3,O2}$^k^S_,H:"&c0  q:#&c0!*,I}|
%|("6$^:"&c0  w^:#&c0!*s_}}:!,F,]<c%#D("$^:#V^k_$^e_("ik:"&c0  q}}|
%|(":!&c0!*-<sum}(&:#k,],M:"&c0  .E:#&c0!*,L:"&c0  .E}_}:!&c0!* |
%|: ;8/<:#;bP8fth: ;8/=:";bP8 :!-<-<:" :! $^:#V^k_:!,F,]<2^1("2|
%|:"&c0  p}}<c" #)$# b'4<2(&n$^b^$^q_^.B(#:#&c0!*3,O2}$^k^S_,H$|
%|^:"&c0  q_^.B:#&c0!*,I}("6$^:"&c0  w^:#&c0!*s_}}$^e_(#,Mik$^:"&c0  q|
%|_^.B}:#&c0!*d$^:"&c0  q_^.B}& b!( b"0 b#8 b$@ b%H b&P!WW}^:!&c0!*0|
%|("2:"&c0  p}}:#&c0!* : ;8/<:#;bP8ft: ;8/=;bP8.K|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{subequations}\begin{align} {\nu {\beta }_{\theta }^{3/2}{k}_{S}(\theta ) \over 6{\omega }_{s}}&=\sum\limits_{k=
-\infty ,\infty }^{} {V}_{k}{e}^{ik\theta } \label{fth} \\  {V}_{k}&={1 \over 2\pi }\int_{0}^{2\pi }{\nu {\beta }_{{\theta }^{\prime }}^{3/2}{k}_{S}({\theta }^{\prime }) \over 6{\omega }_{s}}{e}^{-ik{\theta }^{\prime }}d{\theta }^{\prime }\ \label{ft}\end{align}\end{subequations}
%
%
the Hamiltonian becomes:
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"K:!,F,]"#Symbol^:#&c0  n:"&c0!*J,K<c%#D)## b'4<2^$^V^k^|
%|m^8}$^e_("ik:#&c0  q}<c!0Q^:"&c0!*m}}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|(":!-<sum}(#:"k,Lm}_}"$Monaco^:$   where   [$" )!# b'4<c!0Q^:"3}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW})'# b'4:!,]$^:#&c0  F^:"&c0!*,K|
%|^3:!-<hphantom-[$^:#&c0  F^:"&c0!*,M_:!-]|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW})!# b'4<c!0Q(":",M3}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW})'# b'4:!,]$^:#&c0  F^:"&c0!*,M|
%|^3:!-<hphantom-[$^:#&c0  F^:"&c0!*,M_:!-]|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW})!# b'4<c!0Q^:"1}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW})$# b'4:!,]3$^:#&c0  F^:"&c0!*,K|
%|^2$^:#&c0  F^:"&c0!*,M_}& b!( b"0 b#8 b$@ b%H b&P!WW})!# b'4<c!0Q|
%|(",M1}}}& b!( b"0 b#8 b$@ b%H b&P!WW})$# b'4:!,]3$^:#&c0  F^:"&c0!*,K|
%|_$^:#&c0  F^:"&c0!*,M^2}& b!( b"0 b#8 b$@ b%H b&P!WW}}: -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} K&=\nu J+\sum\limits_{k,m}^{} {{V}_{k}^{m} \over 8}{e}^{ik\theta }\left|{m}\right\rangle~~~{\rm w}{\rm h}{\rm e}{\rm r}{\rm e}~~~\begin{matrix}\left|{3}\right\rangle&={\Phi }_{+}^{3}\hphantom{{\Phi }_{-}}\\
\left|{-3}\right\rangle&={\Phi }_{-}^{3}\hphantom{{\Phi }_{-}}\\
\left|{1}\right\rangle&=3{\Phi }_{+}^{2}{\Phi }_{-}\\
\left|{-1}\right\rangle&=3{\Phi }_{+}{\Phi }_{-}^{2}\end{matrix}\end{align}


In general, the sum over $k$ is infinite and must be truncated. Since our magnets are near Dirac delta functions, it is not clear where we need to stop. In astronomy,   where this  theory originated, the number of harmonics is small. 
However here our goal is to compare to the map based methods of \eq{avesum}. So we will keep all the Fourier terms, all the way to infinity! 
%
%]|Expr|[#b @`b___})b I# b'4" Chicago^: ;bP8&c0!*.["!Helvetica|:! |
%|$^""*|:"K^"#Symbol^:#&c0  q(!:"&c0!*new}:!,F,] -<exp<c!$1(#:",Z|
%|$^F^:#&c0  q_:"&c0!*,Z}}$^K^:#&c0  q_:"&c0!* ,K <c%#D("<2^$(#|
%|,Z$^F^:#&c0  q_:"&c0!*,Z}_(#n,M1}("n,A}}<2(":#&c0  .V$^:"&c0!*F|
%|^:#&c0  q_}(".Vq}}}(":!&c0!*-<sum}(#:"n,]1}^:#&c0  .E}: ;8&c0!*/<|
%|:";bP8finf: ;8/=:#;bP8 :!-<-<:# :! ,F:#&c0  .[$^:"&c0!*K^:#&c0  q|
%|_:"&c0!* ,K<c!=Q)## b'4$^F^:#&c0  q_:"&c0!*,L$^K^:#&c0  q_|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}:"&c0!*,K<2(":#&c0  .V$^:"&c0!*F|
%|^:#&c0  q_}(".Vq}}:"&c0!* ,K "$Monaco^:$Order<c!$1^$^<c!1!^$^|
%|:"F^:#&c0  q_}_^:"&c0!*2}'b M: ;8/<:";bP8f1: ;8/=:!;bP8  : .K|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{subequations}\begin{align} {K}_{\theta }^{new}&= \exp\left({:{F}_{\theta }:}\right){K}_{\theta }\ +\ \sum\limits_{n=
1}^{\infty } {{:{F}_{\theta }:}^{n-1} \over n!}{\partial {F}_{\theta } \over \partial \theta }\label{finf} \\  &\approx {K}_{\theta }\ +\left[{{F}_{\theta },{K}_{\theta }}\right]+{\partial {F}_{\theta } \over \partial \theta }\ +\ {\rm O}{\rm r}{\rm d} {\rm e}{\rm r}\left({{\left|{{F}_{\theta }}\right|}^{2}}\right)\cdots\label{f1}  \end{align}\end{subequations}
\eq{finf} is the exact effect of a canonical transformation represented by  the Lie  transform 
%
%]|Expr|[#b @`b___})$# b'4" Helvetica|: ;bP8&c0!*,D-<exp<c!$1(#|
%|"!*|:!,Z$^F^""Symbol^:"&c0  q_:!&c0!*,Z}}: ,D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
$\exp\left({:{F}_{\theta }:}\right)$ 
on the Hamiltonian ${K}_{\theta}$.
\eq{f1} is a first order expansion of \eq{finf} which is sufficient for our needs. To proceed further we expand ${F}_{\theta }$ in a Fourier series as well
%
%]|Expr|[#b @`b___})'# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"F^"#Symbol^:#&c0  q_:!&c0!*,F,]<c%#D)## b'4$^:"F^k^m$|
%|^e_("ik:#&c0  q}<c!0Q^:"&c0!*m}}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|(":!-<sum}(#:"k,Lm}_}:! : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {F}_{\theta }&=\sum\limits_{k,m}^{} {F}_{k}^{m}{e}^{ik\theta }\left|{m}\right\rangle \end{align}
and substitute the series in \eq{f1}:
%
%]|Expr|[#b @`b___})9# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"K_(!new}:!,F,]"#Symbol^:#&c0  n:"&c0!*J,K<c%#D)## b'4|
%|$^F^k^m$^e_("ik:#&c0  q}<c!=Q($<c!0Q^:"&c0!*m},L:#&c0  n<2("$|
%|^F^:"&c0!*,K_$^:#&c0  F^:"&c0!*,M_}^2}}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!-<sum}(#:"k,Lm}_}"$Monaco^:$,K|
%|<c%#D)$# b'4:"ik$^F^k^m$^e_("ik:#&c0  q}<c!0Q^:"&c0!*m}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!-<sum}(#:"k,Lm}_},K<c%#D)## b'4|
%|<2^$^V^k_^8}$^e_("ik:#&c0  q}<c!0Q^:"&c0!*m}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!-<sum}(#:"k,Lm}_}: ;bP;/":!;bP8 |
%|,F,]:#&c0  n:"&c0!*J,K<c%#D)## b'4<c!(A)%# b'4i<c!$1($k,Mm:#&c0  n}}|
%|$^:"&c0!*F^k^m,K<2^$^V^k_^8}}& b!( b"0 b#8 b$@ b%H b&P!WW}}$^|
%|e_("ik:#&c0  q}<c!0Q^:"&c0!*m}}& b!( b"0 b#8 b$@ b%H b&P!WW}("|
%|:!-<sum}(#:"k,Lm}_},K'b M: ;8/<:";bP8finalhf: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {K}^{new}&=\nu J+\sum\limits_{k,m}^{} {F}_{k}^{m}{e}^{ik\theta }\left[{\left|{m}\right\rangle,\nu {{\Phi }_{+}{\Phi }_{-} \over 2}}\right]+\sum\limits_{k,m}^{} ik{F}_{k}^{m}{e}^{ik\theta }\left|{m}\right\rangle+\sum\limits_{k,m}^{} {{V}_{k} \over 8}{e}^{ik\theta }\left|{m}\right\rangle\nonumber \\
 &=\nu J+\sum\limits_{k,m}^{} \left\{{i\left({k-m\nu }\right){F}_{k}^{m}+{{V}_{k} \over 8}}\right\}{e}^{ik\theta }\left|{m}\right\rangle+\cdots\label{finalhf}\end{align}
Because we have an actual functional form for the Hamiltonian, albeit an infinite series, we can solve for  $F$:
%
%]|Expr|[#b @`b___})8# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"F^k^m:!,F,]i<2^$^:"V^k_)"# b'48<c!$1($k,Mm"#Symbol^:#&c0  n}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}:"&c0!*    :!-<Longrightarrow |
%|   $^F^:#&c0  q_:"&c0!*,]<c%#D)## b'4<2("i$^V^k_})"# b'48<c!$1|
%|($k,Mm:#&c0  n}}}& b!( b"0 b#8 b$@ b%H b&P!WW}}$^:"&c0!*e_("ik|
%|:#&c0  q}<c!0Q^:"&c0!*m}}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!-<sum}|
%|(#:"k,Lm}_}:! : ;8/<:";bP8ffourier: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {F}_{k}^{m}&=i{{V}_{k} \over 8\left({k-m\nu }\right)}\ \ \ \ \Longrightarrow    {F}_{\theta }=
\sum\limits_{k,m}^{} {i{V}_{k} \over 8\left({k-m\nu }\right)}{e}^{ik\theta }\left|{m}\right\rangle \label{ffourier}\end{align}
\fbox{
\vbox{  {\large \bf  Important points}: If we could keep a few terms in \eq{ffourier}, as is possible in astronomy, then we could put the result for $F$ into the exact form for $K^{new}$, namely
\eq{finf}, and expand this to a high order. We could repeat the process iteratively until the $\theta $ (or $s$) dependent  canonical transformation is found to a given order in the perturbation.
This is a powerful result and it is often implemented to high order in astronomy. With Lie methods discovered by Hori, the algebraic manipulations were pioneered by Andr\'e Deprit but in facts they  go back to the nineteenth century under the name of secular perturbation theory.
}
}
\v5
We want to show that the answer in \eq{ffourier} is the same as the map result of \eq{fanst}. Therefore we must keep all the terms all the way to infinity in the Fourier transform! Let us substitute the expression for $V_k$ in \eq{ffourier}:
%
%]|Expr|[#b @`b___})5# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^F^""Symbol^:"&c0  q_:!&c0!*,F"#*|:#,]<c%#D)## b'4<2)## b'4i|
%|<2^:!1("2:"&c0  p}}<c" #)$# b'4<2(&n$^b^$^q_^.B(#:#&c0!*3,O2}|
%|$^k^S_,H$^:"&c0  q_^.B:#&c0!*,I}("48$^:"&c0  w^:#&c0!*s_}}$^e|
%|_(#,Mik$^:"&c0  q_^.B}:#&c0!*d$^:"&c0  q_^.B|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^:!&c0!*0("2:"&c0  p}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW})!# b'4<c!$1($:#&c0!*k,Mm:"&c0  n}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}$^:#&c0!*e_("ik:"&c0  q}<c!0Q^|
%|:#&c0!*m}}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!-<sum}(#:#k,Lm}_}:! |
%|: ;bP;/":!;bP8 ,F,]<c" #)'# b'4:#i<2^:!1("2:"&c0  p}}<2(&n$^b|
%|^$^q_^.B(#:#&c0!*3,O2}$^k^S_,H$^:"&c0  q_^.B:#&c0!*,I}("48$^:"&c0  w|
%|^:#&c0!*s_}}d$^:"&c0  q_^.B<c%#D)"# b'4<2)!# b'4$^:#&c0!*e_(#|
%|,Mik$^:"&c0  q_^.B}}& b!( b"0 b#8 b$@ b%H b&P!WW})!# b'4<c!$1|
%|($:#&c0!*k,Mm:"&c0  n}}}& b!( b"0 b#8 b$@ b%H b&P!WW}}$^:#&c0!*e|
%|_("ik:"&c0  q}}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!&c0!*-<sum}(#|
%|:#k,Lm}_}<c!0Q^m}}& b!( b"0 b#8 b$@ b%H b&P!WW}^0("2:"&c0  p}}|
%|:!&c0!* : ;bP;/":!;bP8 ,F,]<c" #)%# b'4<2(&:"&c0  n$^b^$^q_^.B|
%|(#:#&c0!*3,O2}$^k^S_,H$^:"&c0  q_^.B:#&c0!*,I}(#48$^:"&c0  w^|
%|:#&c0!*s_}}d$^:"&c0  q_^.B<c%#D^<c%"C^<cY A("<2(":#&c0!*i$^e_|
%|)$# b'4,Mim:"&c0  n<c!$1(#$^q_^.B:#&c0!*,M:"&c0  q}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}(":!&c0!*2:"&c0  p}}<c%#D)!# b'4|
%|<2)!# b'4$^:#&c0!*e_)$# b'4,Mi<c!$1($k,Mm:"&c0  n}}<c!$1(#$^q|
%|_^.B:#&c0!*,M:"&c0  q}}}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW})!# b'4<c!$1($:#&c0!*k,Mm:"&c0  n}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|(":!&c0!*-<sum}^:#k_}}}^$^G(!mk}__}(":!-<sum}^:#m_}<c!0Q^m}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^0("2:"&c0  p}}:!&c0!* : ;8/<:#;bP8gmk|
%|: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {F}_{\theta }&=
\sum\limits_{k,m}^{} {i{1 \over 2\pi }\int_{0}^{2\pi }{\nu {\beta }_{{\theta }^{\prime }}^{3/2}{k}_{S}({\theta }^{\prime }) \over 48{\omega }_{s}}{e}^{-ik{\theta }^{\prime }}d{\theta }^{\prime } \over \left({k-m\nu }\right)}{e}^{ik\theta }\left|{m}\right\rangle \nonumber \\
 &=\int_{0}^{2\pi }i{1 \over 2\pi }{\nu {\beta }_{{\theta }^{\prime }}^{3/2}{k}_{S}({\theta }^{\prime }) \over 48{\omega }_{s}}d{\theta }^{\prime }\sum\limits_{k,m}^{} {{e}^{-ik{\theta }^{\prime }} \over \left({k-m\nu }\right)}{e}^{ik\theta }\left|{m}\right\rangle \nonumber \\
 &=\int_{0}^{2\pi }{\nu {\beta }_{{\theta }^{\prime }}^{3/2}{k}_{S}({\theta }^{\prime }) \over 48{\omega }_{s}}d{\theta }^{\prime }\sum\limits_{m}^{} \underbrace{{i{e}^{-im\nu \left({{\theta }^{\prime }-\theta }\right)} \over 2\pi }\sum\limits_{k}^{} {{e}^{-i\left({k-m\nu }\right)\left({{\theta }^{\prime }-\theta }\right)} \over \left({k-m\nu }\right)}}\limits_{{G}_{mk}}^{}\left|{m}\right\rangle \label{gmk}\end{align}




The four terms in \eq{fanst} must be equal to $G_{mk}$ of \eq{gmk}. There is no easy way to sum this horror directly. However, since I know that this can be easily derived by Green's functions, i.e., the rules of \sec{secgreenrule}, I am tempted to take the derivative of  $G_{mk}$ with respect to 
%
%]|Expr|[#b @`b___})'# b'4" Helvetica|: ;bP8&c0!*,D"!Symbol^:!&c0  t|
%|""*|:"&c0!*,]:!&c0  q:"&c0!*,M$^:!&c0  q_^.B: &c0!*,D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
$\tau =
{\theta }^{\prime }-\theta $, hoping that a Dirac-delta function will emerge:
%
%]|Expr|[#b @`b___})b B# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<2^$("""Symbol^:"&c0  .V"#*|:#&c0!*G}(!mk}_(":"&c0  .Vt}}:!&c0!*,F,]|
%|<2^$(&:"&c0  .V:!&c0!*-<hphantom-[:"&c0  t:!&c0!*-]}__(":"&c0  .V|
%|t}}<2(":#&c0!*i$^e_)## b'4,Mim:"&c0  nt|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}(":!&c0!*2:"&c0  p}}<c%#D)!# b'4|
%|<2)!# b'4$^:#&c0!*e_)$# b'4,Mi<c!$1($k,Mm:"&c0  n}}t|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}& b!( b"0 b#8 b$@ b%H b&P!WW})!# b'4|
%|<c!$1($:#&c0!*k,Mm:"&c0  n}}}& b!( b"0 b#8 b$@ b%H b&P!WW}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!&c0!*-<sum}^:#k_}: ;bP;/":!;bP8 |
%|,F,],Mim$^:#G(!mk}_,K$^e_)## b'4,Mim:"&c0  nt|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}<c%"C^<cY A("<2^:#&c0!*1(":!2:"&c0  p}}|
%|<c%#D)!# b'4$^:#&c0!*e_)$# b'4,Mi<c!$1($k,Mm:"&c0  n}}t|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}& b!( b"0 b#8 b$@ b%H b&P!WW}("|
%|:!&c0!*-<sum}^:#k_}}})## b'4:"&c0  d<c!$1^t}$^:#&c0!*e_)"# b'4|
%|im:"&c0  nt}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_}:!&c0!* : ;bP;/":!;bP8 ,F:"&c0  /?|
%|: ;bP;&c0!*/":!;bP8 <2^$(":"&c0  .V:#&c0!*G}(!mk}_(":"&c0  .V|
%|t}}:!&c0!*,F,],Mim$^:#G(!mk}_,K:"&c0  d<c!$1^t}:#&c0!* : ;8/<|
%|:#;bP8dgmk: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {{\partial G}_{mk} \over \partial \tau }&={{\partial \hphantom{\tau }}^{} \over \partial \tau }{i{e}^{-im\nu \tau } \over 2\pi }\sum\limits_{k}^{} {{e}^{-i\left({k-m\nu }\right)\tau } \over \left({k-m\nu }\right)}\nonumber \\
 &=-im{G}_{mk}+{e}^{-im\nu \tau }\underbrace{{1 \over 2\pi }\sum\limits_{k}^{} {e}^{-i\left({k-m\nu }\right)\tau }}\limits_{\delta \left({\tau }\right){e}^{im\nu \tau }}^{} \nonumber \\
 &\Downarrow \nonumber \\
 {{\partial G}_{mk} \over \partial \tau }&=-im{G}_{mk}+\delta \left({\tau }\right)\ \label{dgmk}\end{align}
To solve \eq{dgmk}, we first solve outside the delta function:
%
%]|Expr|[#b @`b___}),# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"G(!mk}_:!,F"#Symbol^:#&c0  .U, l$^:"&c0!*e_(#,Mim:#&c0  nt}|
%|: ;8&c0!*/<:";bP8gmkt: ;8/=:!;bP8 : -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {G}_{mk}&\propto \lambda {e}^{-im\nu \tau }\label{gmkt} \end{align}
To fix $\lambda $ we integrate around $\tau =0$:
%
%]|Expr|[#b @`b___})5# b'4" Chicago^: ;bP8&c0!*-="!Monaco^:!for|
%| ""Symbol^:"&c0  e.N"#*|:#&c0!*0,Z  "$Helvetica|:$ $^:#G(!mk}|
%|_<c!$1^:"&c0  e}:#&c0!*,M$^G(!mk}_<c!$1(",M:"&c0  e}}:#&c0!*,]|
%|1: ;8/<:#;bP8diraccond: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align}{\rm f}{\rm o}{\rm r}~\varepsilon \rightarrow 0:\ \  {G}_{mk}\left({\varepsilon }\right)-{G}_{mk}\left({-\varepsilon }\right)=
1\label{diraccond}\end{align}
However,  ${G}_{mk}$ must be periodic, therefore we must have:
%
%]|Expr|[#b @`b___})3# b'4" Chicago^: ;bP8&c0!*-=$^"!*|:!G(!mk}|
%|_<c!$1(",M""Symbol^:"&c0  e}}"#Helvetica|:#&c0!*,F:!,]$^G(!mk}|
%|_<c!$1($2:"&c0  p:!&c0!*,M:"&c0  e}}:#&c0!*-<Longrightarrow $|
%|^:!G(!mk}_<c!$1($2:"&c0  p:!&c0!*,M:"&c0  e}}:!&c0!*,]:"&c0  l|
%|$^:!&c0!*e_(%,Mim:"&c0  n:!&c0!*2:"&c0  p}:!&c0!*,]:"&c0  l$^|
%|:!&c0!*e_(#,Mim:"&c0  m}: &c0!*-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align}{G}_{mk}\left({-\varepsilon }\right)&=
{G}_{mk}\left({2\pi -\varepsilon }\right)\Longrightarrow {G}_{mk}\left({2\pi -\varepsilon }\right)=
\lambda {e}^{-im\nu 2\pi }=
\lambda {e}^{-im\mu }\end{align}
\Eq{diraccond} is rewritten as 
%
%]|Expr|[#b @`b___})7# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""Monaco^:"for "#Symbol^:#&c0  e.N"$*|:$&c0!*0,Z  :! :#&c0  l|
%|:$&c0!*,M:#&c0  l$^:$&c0!*e_(#,Mim:#&c0  m}:$&c0!*,]1: ;8/<:$;bP8diraccond|
%|2: ;8/=:!;bP8 : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\rm f}{\rm o}{\rm r}~\varepsilon \rightarrow 0:\ \  \lambda -\lambda {e}^{-im\mu }=
1\label{diraccond2} \end{align}
and the final result is thus:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"G(!mk}_:!,F:",]<2^$^e_)$# b'4,Mim"#Symbol^:#&c0  n<c!$1|
%|(#$^q_^.B:"&c0!*,M:#&c0  q}}}& b!( b"0 b#8 b$@ b%H b&P!WW}(#:"&c0!*1|
%|,M$^e_(#,Mim:#&c0  m}}}:"&c0!*,]<2^$^e_)## b'4,Mim:#&c0  D|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}(#:"&c0!*1,M$^e_(#,Mim:#&c0  m}}}|
%|:"&c0!* : ;8/<:";bP8gmktf: ;8/=:!;bP8 : -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {G}_{mk}&=
{{e}^{-im\nu \left({{\theta }^{\prime }-\theta }\right)} \over 1-{e}^{-im\mu }}=
{{e}^{-im\Delta } \over 1-{e}^{-im\mu }}\ \label{gmktf} \end{align}
%
%]|Expr|[#b @`b___})b G# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^F^""Symbol^:"&c0  q_:!&c0!* ,F,]<c" #)%# b'4<2(&:"&c0  n$^b|
%|^$^q_^.B(#"#*|:#&c0!*3,O2}$^k^S_,H$^:"&c0  q_^.B:#&c0!*,I}("48|
%|$^:"&c0  w^:#&c0!*s_}}d$^:"&c0  q_^.B<c%#D)!# b'4<2^$^:#&c0!*e|
%|_)## b'4,Mim:"&c0  D}& b!( b"0 b#8 b$@ b%H b&P!WW}(#:#&c0!*1,M|
%|$^e_(#,Mim:"&c0  m}}}}& b!( b"0 b#8 b$@ b%H b&P!WW}(":!&c0!*-<|
%|sum}^:#m_}<c!0Q^m}}& b!( b"0 b#8 b$@ b%H b&P!WW}^0("2:"&c0  p}}|
%|:!&c0!* : ;bP;/":!;bP8 ,F,]<c" #)%# b'4<2(%$^:"&c0  b^s(#:#&c0!*3|
%|,O2}$^k^S_,H:"&c0  s:#&c0!*,I}(!48}}d:"&c0  s<c%#D)!# b'4<2^$|
%|^:#&c0!*e_)## b'4,Mim:"&c0  D}& b!( b"0 b#8 b$@ b%H b&P!WW}(#|
%|:#&c0!*1,M$^e_(#,Mim:"&c0  m}}}}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|(":!&c0!*-<sum}^:#m_}<c!0Q^m}}& b!( b"0 b#8 b$@ b%H b&P!WW}^0|
%|^C}"$Monaco^:$       since  <2("d$^:"&c0  q_^.B}(":#&c0!*d:"&c0  s}}|
%|:#&c0!*,]<2^$^:"&c0  w^s_^n}:#&c0!*,L   :$Q,NE,ND,N : ;8/<:#;bP8gmkqed|
%|: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {F}_{\theta } &=\int_{0}^{2\pi }{\nu {\beta }_{{\theta }^{\prime }}^{3/2}{k}_{S}({\theta }^{\prime }) \over 48{\omega }_{s}}d{\theta }^{\prime }\sum\limits_{m}^{} {{e}^{-im\Delta } \over 1-{e}^{-im\mu }}\left|{m}\right\rangle \nonumber \\
 &=\int_{0}^{C}{{\beta }_{\sigma }^{3/2}{k}_{S}(\sigma ) \over 48}d\sigma \sum\limits_{m}^{} {{e}^{-im\Delta } \over 1-{e}^{-im\mu }}\left|{m}\right\rangle~~~~~~~{\rm s}{\rm i}{\rm n}{\rm c}{\rm e}~~{{\rm d} {\theta }^{\prime } \over d\sigma }=
{{\omega }_{\sigma } \over \nu },\ \ \ {\rm Q}.{\rm E}.{\rm D}.~\label{gmkqed}\end{align}



\chapter{\color{.} De Moivre's formula and Invariant-Moments Duality}\label{appdemoivre}

The derivation (see \cite{nishisands} for more material) of de Moivre formula only requires the existence of a normal form. We might as well do it in the non-symplectic case. In  3-d-f, when classical radiation is present, the map $M$ can be factorised as 
%
%]|Expr|[#b @`b___})b Z# b'4" Chicago^: ;bP8&c0!*,^"!Helvetica|:! |
%|""*|:"M"#Symbol^:# :!,F:",]:!,F:# :"A:#&c0  L:"&c0!*R$^A_(",M|
%|1}:# : ;8/<:";bP8nfld: ;8/=:!;bP8-<-<:# :! "$Times|:$R:# :!,F|
%|:",]:!,F:# <c!$1^[## ^$^:$r^1_^0^0^0^$^r^2_^0^0^0^$^r^3_}}   |
%|   $^r^i_,]<c!$1^["" ($:!-<cos:$ $^:#&c0  m^:"&c0!*i_}(% :!-<|
%|sin:$ $^:#&c0  m^:"&c0!*i_}(%,M:!-<sin:$ $^:#&c0  m^:"&c0!*i_}|
%|($:!-<cos:$ $^:#&c0  m^:"&c0!*i_}}}: ;bP;/":!;bP8 :#&c0  L&c0!* |
%|:!,F:",]:!,F:# <c!$1^[## ^$^&c0  L^:"&c0!*1_^:$0^0^0^$^:#&c0  L|
%|^:"&c0!*2_^:$0^0^0^$^:#&c0  L^:"&c0!*3_}}:$      $^:#&c0  L^:"&c0!*i|
%|_:$,]<c!$1^["" )## b'4:!-<exp<c!$1(":",M$^:#&c0  a^:"&c0!*i_}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^0^0)## b'4:!-<exp<c!$1(":",M$^|
%|:#&c0  a^:"&c0!*i_}}}& b!( b"0 b#8 b$@ b%H b&P!WW}}}: ;8/<:";bP8nfldl|
%|: ;8/=:!;bP8 : ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray} M &=
& A\Lambda R{A}^{-1} \label{nfld}\\  R &=
& \left({\begin{matrix}{r}_{1}&0&0\\
0&{r}_{2}&0\\
0&0&{r}_{3}\end{matrix}}\right)\ \ \ \ \ \ {r}_{i}=
\left({\begin{matrix}\cos\ {\mu }_{i}&\ \sin\ {\mu }_{i}\\
-\sin\ {\mu }_{i}&\cos\ {\mu }_{i}\end{matrix}}\right)\nonumber \\
 \Lambda  &=
& \left({\begin{matrix}{\Lambda }_{1}&0&0\\
0&{\Lambda }_{2}&0\\
0&0&{\Lambda }_{3}\end{matrix}}\right)\ \ \ \ \ \ {\Lambda }_{i}=
\left({\begin{matrix}\exp\left({-{\alpha }_{i}}\right)&0\\
0&\exp\left({-{\alpha }_{i}}\right)\end{matrix}}\right)\label{nfldl} \end{eqnarray}
%
%
%
Applying \eq{nfldl} to \eq{nfld} we get
%
%]|Expr|[#b @`b___});# b'4" Chicago^: ;bP8&c0!*,^"!*|:!M""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" :!A<c!$1^<c%#D("$^e_(",M$^:"&c0  a|
%|^:!&c0!*i_}<c!(A(*:#-<cos<c!$1^$^:"&c0  m^:!&c0!*i_}$^I_^i,K:#-<|
%|sin<c!$1^$^:"&c0  m^:!&c0!*i_}S$^I_^i}}}(":#-<sum}^:!i_}}$^A_|
%|(",M1}:" : ;8/<:!;bP8dma: ;8/=:#;bP8-<-<:" :# ,F:!,]:#,F:" <c%#D|
%|("$^:!e_(",M$^:"&c0  a^:!&c0!*i_}<c!(A))# b'4:#-<cos<c!$1^$^:"&c0  m|
%|^:!&c0!*i_}<c%"C^<cY A(#A$^I_^i$^A_(",M1}}}^$^H_^i_},K:#-<sin|
%|<c!$1^$^:"&c0  m^:!&c0!*i_}<c%"C^<cY A(#AS$^I_^i$^A_(",M1}}}^|
%|$^B_^i_}}& b!( b"0 b#8 b$@ b%H b&P!WW}}}(":#-<sum}^:!i_} : ;8/<|
%|:!;bP8dmb: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}M &=
& A\left({\sum\limits_{i}^{} {e}^{-{\alpha }_{i}}\left\{{\cos\left({{\mu }_{i}}\right){I}^{i}+\sin\left({{\mu }_{i}}\right)S{I}^{i}}\right\}}\right){A}^{-1} \label{dma}\\  &=
& \sum\limits_{i}^{} {e}^{-{\alpha }_{i}}\left\{{\cos\left({{\mu }_{i}}\right)\underbrace{A{I}^{i}{A}^{-1}}\limits_{{H}^{i}}^{}+\sin\left({{\mu }_{i}}\right)\underbrace{AS{I}^{i}{A}^{-1}}\limits_{{B}^{i}}^{}}\right\}\ \label{dmb}\end{eqnarray}
%
where $I^i$ is the identity only in the $i^{th}$ plane. For example,
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!I_^2 |
%|,] ""Helvetica|:"-<mbox-[-<scriptsize ,D<c!$1^[&& ^"#Times|:#0|
%|^0^0^:!0^0^0^:#0^0^0^:!0^0^0^:#0^0^1^:!0^0^0^0^0^0^1^0^0^0^0^|
%|0^0^0^0^0^0^0^0^0^0}}:",D-]: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{I}^{2}\ =
\ \mbox{\scriptsize $\left({\begin{matrix}0&0&0&0&0&0\\
0&0&0&0&0&0\\
0&0&1&0&0&0\\
0&0&0&1&0&0\\
0&0&0&0&0&0\\
0&0&0&0&0&0\end{matrix}}\right)$}\end{eqnarray}
%
%
%
%
and $S$ is the usual symplectic form\footnote{It is often $J$ in the literature, but we prefer $S$ because of the omnipresence of the action variable $J$ in accelerator theory.}:
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*,^"!*|:!S ,] ""Helvetica|:"-<|
%|mbox-[-<scriptsize ,D<c!$1^[&& ^"#Times|:#0^1^0^:!0^0^0(":#,M|
%|1}^0^0^:!0^0^0^:#0^0^0^:!1^0^0^0^0(",M1}^0^0^0^0^0^0^0^:#0^:!1|
%|^0^0^0^0(",M1}^0}}:",D-]: ;8/<:!;bP8symps: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}S\ =
\ \mbox{\scriptsize $\left({\begin{matrix}0&1&0&0&0&0\\
-1&0&0&0&0&0\\
0&0&0&1&0&0\\
0&0&-1&0&0&0\\
0&0&0&0&0&1\\
0&0&0&0&-1&0\end{matrix}}\right)$}\label{symps}\end{eqnarray}
%

It is easy to show that the matrices %
${H}^i$
and
%
%]|Expr|[#b @`b___})## b'4" Helvetica|: ;bP8&c0!*,D$^"!*|:!B_^|
%|<c!$1^i}: ,D}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
${B}^i$
form three independent representations of the complex numbers $1$ and $i$:
%
%]|Expr|[#b @`b___})b U# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!H_^|
%|i$^H_^j""Helvetica|:",F:!,]:",F$^"#Symbol^:#&c0  d(!:!&c0!*ij}|
%|_$^H_^j : ;8/<:!;bP8etabetaprop1: ;8/=:#;bP8  :"-<-<:# :" :# |
%|:! $^H_^i$^B_^j:",F:!,]:",F$^:!B_^j$^H_^i,]$^:#&c0  d(!:!&c0!*ij}|
%|_$^B_^j : ;8/<:!;bP8etabetaprop2: ;8/=:#;bP8 :"-<-<:# :! $^B_|
%|^i$^B_^j:",F:!,]:",F:!,M$^:#&c0  d(!:!&c0!*ij}_$^H_^j:" : ;8/<|
%|:!;bP8etabetaprop3: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{H}^{i}{H}^{j}&=
&{\delta }_{ij}{H}^{j}\ \label{etabetaprop1}  \\   \ {H}^{i}{B}^{j}&=
&{B}^{j}{H}^{i}=
{\delta }_{ij}{B}^{j}\ \label{etabetaprop2} \\ \ {B}^{i}{B}^{j}&=
&-{\delta }_{ij}{H}^{j} \label{etabetaprop3}\end{eqnarray}
%
%
De Moivre's general formula follows from the three previous equations. 
%
%]|Expr|[#b @`b___}),# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!M_^N""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F<c%#D("$^:!e_(#,MN$^:"&c0  a^:!&c0!*i|
%|_}<c!(A))# b'4:#-<cos<c!$1(":!N$^:"&c0  m^:!&c0!*i_}}$^H_^i,K|
%|:#-<sin<c!$1(":!N$^:"&c0  m^:!&c0!*i_}}$^B_^i|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}}(":#-<sum}^:!i_} : ;8/<:!;bP8dmbn|
%|: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{M}^{N} &=
&\sum\limits_{i}^{} {e}^{-N{\alpha }_{i}}\left\{{\cos\left({N{\mu }_{i}}\right){H}^{i}+\sin\left({N{\mu }_{i}}\right){B}^{i}}\right\}\ \label{dmbn}\end{eqnarray}
%
Moreover, we can again rewrite the map $M$ in terms of a single exponent:%
%
%]|Expr|[#b @`b___})2# b'4" Chicago^: ;bP8&c0!*,^"!*|:!M""Helvetica|:" |
%|,F:!,]:",F"#Symbol^:# :"-<exp<c!$1^<c%#D(&:!,M$^:#&c0  a^:!&c0!*i|
%|_$^H_^i,K$^:#&c0  m^:!&c0!*i_$^B_^i}(":"-<sum}^:!i_}},]:"-<exp|
%|<c!$1^<c%#D(':!,M$^:#&c0  a^:!&c0!*i_$^H_^i,K$^:#&c0  m^:!&c0!*i|
%|_S$^K_^i}(":"-<sum}^:!i_}}: ;8/<:!;bP8cslie: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}M &=
& \exp\left({\sum\limits_{i}^{} -{\alpha }_{i}{H}^{i}+{\mu }_{i}{B}^{i}}\right)=
\exp\left({\sum\limits_{i}^{} -{\alpha }_{i}{H}^{i}+{\mu }_{i}S{K}^{i}}\right)\label{cslie}\end{eqnarray}
%
There are a few comments we need to make concerning the results of this section. 
\begin{enumerate} 
\item As shown by \eq{cslie}, the lattice functions
${H}^i$
 cannot be removed easily from any treatment of a non-symplectic system. Of course, using \eq{etabetaprop3}, we can 
replace them by quadratic polynomials in the 
${B}^i$'s. 
\item Forest, in \cite{displat}, has  shown that analytical tune shift formulas for an arbitrary force depend on the ${B}^i$'s while the damping shifts depend on the ${H}^i$'s.
\item In a symplectic system, if perturbation theory is done on the Hamiltonian, the lattice functions ${H}^i$ will not naturally appear since the exponent in
\eq{cslie} only depends of the ${B}^i$'s (when the damping is zero). This is why they never appear in Ripken's extensive literature on the subject.

\end{enumerate}

Now let us look at the symplectic case, the matrices $B^i$, $K^i$ and the quadratic moment matrices $E^i$ defined as
%
%]|Expr|[#b @`b___}))# b'4" Chicago^: ;bP8&c0!*,^<c!,Q("$^"!Times|:!z|
%|^a_$^z^b_}}""Helvetica|:",F,],F<c%#D("$^"#*|:#E(!ab}^i<c!,Q^$|
%|^J^i_}}(":"-<sum}(%:#i,]1,L3}_}:" : ;8/<:#;bP8momg: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}\left\langle{{z}_{a}{z}_{b}}\right\rangle&=&\sum\limits_{i=
1,3}^{} {E}_{ab}^{i}\left\langle{{J}_{i}}\right\rangle \label{momg}\end{eqnarray}
 are all simply related.


%
%]|Expr|[#b @`b___})b F# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!B_^|
%|i""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" :!S$^K_^i:# :" : ;8/<|
%|:!;bP8inv: ;8/=:#;bP8-<-<:" $^:!B_^i:#,F:!,]:#,F$^:!E_^iS:" : ;8/<|
%|:!;bP8mom: ;8/=:#;bP8-<-<:" $^:#K_^i,F,],FS$^E_^i$^S_^: t:" : ;8/<|
%|:!;bP8mominv: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{B}^{i} &=
& S{K}^{i}  \label{inv}\\ {B}^{i}&=
&{E}^{i}S \label{mom}\\ {K}^{i}&=&S{E}^{i}{S}^{\rm T} \label{mominv}\end{eqnarray}
%


%
For example, the pseudo-Hamiltonian which generates the matrix $M$ is in the sum of the three invariants weighted by the tunes:%
%
%]|Expr|[#b @`b___})b N# b'4" Chicago^: ;bP8&c0!*,^H"!Helvetica|:!,F,],F|
%|<c%#D($<2^$^""Symbol^:"&c0  m^"#*|:#&c0!*i_^:!2}$^:#z_^: t$^:#K|
%|_^iz}(":!-<sum}(%:#i,]1,L3}_},]<c%#D(#<2^$^:"&c0  m^:#&c0!*i_|
%|^:!2}$^:"&c0  e^:#&c0!*i_<c!$1^z}}(":!-<sum}(%:#i,]1,L3}_}:! |
%|: ;8/<:#;bP8pseudoh: ;8/=:";bP8 :!-<-<:" : ;bP;/":!;bP8 $^:"&c0  e|
%|^:#&c0!*i_<c!$1^z}:!,F,],F $^:#z_^: t$^:#K_^iz"$Times|:$     |
%| :"&c0  .L:#&c0!*      "%Monaco^:%Coupled Courant,MSnyder Invariants|
%|: ;8/<:#;bP8ccsi: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{\cal H}&=&\sum\limits_{i=
1,3}^{} {{\mu }_{i} \over 2}{z}^{\rm T}{K}^{i}z=
\sum\limits_{i=
1,3}^{} {{\mu }_{i} \over 2}{\varepsilon }_{i}\left({z}\right) \label{pseudoh} \\ \nonumber \\
 {\varepsilon }_{i}\left({z}\right)&=& {z}^{\rm T}{K}^{i}z\ \ \ \ \ \ \leftarrow \ \ \ \ \ \ {\rm C}{\rm o}{\rm u}{\rm p}{\rm l}{\rm e}{\rm d} ~{\rm C}{\rm o}{\rm u}{\rm r}{\rm a}{\rm n}{\rm t}-{\rm S}{\rm n}{\rm y}{\rm d} {\rm e}{\rm r}~{\rm I}{\rm n}{\rm v}{\rm a}{\rm r}{\rm i}{\rm a}{\rm n}{\rm t}{\rm s}\label{ccsi}\end{eqnarray}
%

%
$\cal H$ is just the Lie operator for the one-turn linear map. So here is the moral of the story: if you can measure the moments ($E^i$'s) and the tunes, you have pseudo-Hamiltonian ($K^i$'s). Finally if you have the pseudo-Hamiltonian, you also have the matrix ($B^i$'s).


\chapter{ \color{.} The case of spin}\label{appspin}

We will now describe the normal form in FPP for the spin orbital map when the spin is a spectator. Again it is simplest to describe it in words first. In the commutative normal form of the spin orbital map, the orbital part moves on its usual circles with an amplitude dependent tune. Nothing is new there. The spin maps are all rotations along an arbitrary axis--- we pick the vertical  $y$-axis. The angle of the spin rotation depends only of the orbital motion, i.e., on the radii of the circles--- on the action variables!

This is so trivial that a child can understand it. First of all, the $y$-axis is certainly an invariant. The spin angle being a function of the actions $J_i$ is certainly a well defined tune since it is constant along the trajectory. It is the so-called spin tune. Finally since it is a commutative normal form, we can define easily a spin phase advance and a spin adiabatic invariant. But we are going too far here. 

The normalised motion in the presence of spin is like the small and large arms of an old fashion watch. The small arm marks the time (orbital) and the little arm  moves at a faster rate totally related to the hourly rate of the small arm. The amplitude dependence would be like saying that making the clock smaller/larger in size would result in a clock not well adjusted. Actually that would probably be the case if one scales the size of the clock without retuning it.


\textcolor{red}{\bf In the rest of the text we may use the locution ``normal norm'' to describe the Taylor map algorithm of FPP. The reader will perhaps excuse the abuse of language. Always one should remember that the tracking code is the final arbiter on the validity of an algorithmically computed normal form.}

\section{\color{.}  Spin Maps in FPP}

In FPP spin map are of the type ``DAMAPSPIN''. They are defined as follows:

\begin{verbatim} 
  TYPE DAMAPSPIN
     TYPE(DAMAP) M
     TYPE(REAL_8) S(3,3)
      real(dp) e_ij(6,6)  ! Envelope radiation stuff
  END TYPE DAMAPSPIN
\end{verbatim} 

\section{\color{.}  Spin Maps Concatenation}

\noindent So if $T$ is a spin map, it is then given by a couple of objects: 
%
%]|Expr|[#b @`b___})%# b'4" Chicago^: ;bP8&c0!*,^"!Times|:!T""*|:",]|
%|<c!$1(#m,LS}}: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}T=
\left({m,S}\right)\end{eqnarray}
%
where $m$ is an orbital map and $S$ is an orbital dependent spin matrix. If a beam line \#1 is followed by beam line \#2, then the spin map for the full beam line is given by:
%
%]|Expr|[#b @`b___})9# b'4" Chicago^: ;bP8&c0!*,^$^"!Times|:!T|
%|^2_""Symbol^:"&c0  .P, $^:!&c0!*T^1_:"&c0  &c0!* "#Helvetica|:#,F|
%|"$*|:$,]:#,F:" <c!$1(#$(":$m}^2_,L$^S^2_}}:"&c0  .P<c!$1(#$^:$&c0!*m|
%|^1_,L$^S^1_}}: ;bP;/":#;bP8 :" :#,F:$,]:#,F:" <c!$1(($^:$m^2_|
%|:"&c0  .P$^:$&c0!*m^1_,L$^S^2_:"&c0  .P$^:$&c0!*m^1_$^S^1_}}: ;8/<|
%|:$;bP8concat: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{T}_{2}\circ {T}_{1} &=
& \left({{m}_{2},{S}_{2}}\right)\circ \left({{m}_{1},{S}_{1}}\right)\nonumber \\
  &=
& \left({{m}_{2}\circ {m}_{1},{S}_{2}\circ {m}_{1}{S}_{1}}\right)\label{concat}\end{eqnarray}
%
The matrix 
%
%]|Expr|[#b @`b___})(# b'4" *|: ;bP8&c0!*-@-@"!Helvetica|:!,D$|
%|^: S^2_""Symbol^:"&c0  .P$^: &c0!*m^1_:"&c0  .T$^: &c0!*S^1_:!,D,G,G|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
``${S}_{2}\circ {m}_{1}\times {S}_{1}$''
 is simply the product of $S_2 S_1$ where $S_2(\overline{z})$ is evaluated at $\overline{z}=m_2(z)$ with $z=(x,p_x,y,p_y,z_5,z_6)$.

\section{\color{.}  Spin Normal Form}

Let us imagine that a spin-orbit transformation $U=(a,A)$ such that:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*,^"!*|:!T,]<c!$1|
%|((m,LS""Helvetica|:"-<vphantom-[$^:!e_(""#Symbol^:#&c0  q$^:!&c0!*L|
%|^y_}:"-]}}:!,]<c%"C^<cY A^<c!$1((a,LA:"-<vphantom-[$^:!e_(":#&c0  q|
%|$^:!&c0!*L^y_}:"-]}}}^:!U_}:#&c0  .P<c%"C^<cY A^<c!$1(#:!&c0!*r|
%|,L$^e_(":#&c0  q$^:!&c0!*L^y_}}}}^R_}:#&c0  .P<c%"C^<cY A^$^<c!$1|
%|((:!&c0!*a,LA:"-<vphantom-[$^:!e_(":#&c0  q$^:!&c0!*L^y_}:"-]}}|
%|_(":!,M1}}^$^U_(",M1}_}: ;8/<:!;bP8normalspin: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}T=
\left({m,S\vphantom{{e}^{\theta {L}_{y}}}}\right)=
\underbrace{\left({a,A\vphantom{{e}^{\theta {L}_{y}}}}\right)}\limits_{U}^{}\circ \underbrace{\left({r,{e}^{\theta {L}_{y}}}\right)}\limits_{R}^{}\circ \underbrace{{\left({a,A\vphantom{{e}^{\theta {L}_{y}}}}\right)}^{-1}}\limits_{{U}^{-1}}^{}\label{normalspin}\end{eqnarray}
%
It is clear that the map $R$ can be formally computed as a Taylor map in both orbital and spin variables around the origin. It is now implemented in FPP. The type normal\_spin  is given by: 
{\footnotesize
\begin{verbatim}
  type normal_spin
     type(normalform) N   ! regular orbital normal form
     type(damapspin) a1   ! brings to fixed point
     type(damapspin) ar   ! normalises around the fixed point
     type(damapspin) as   ! pure spin map 
     type(damapspin) a_t  ! !! (a_t%m,a_t%s) = (a1%m, I ) o (I ,as%s) o (ar%m,I)
!!!  extra spin info
     integer M(NDIM,NRESO),MS(NRESO),NRES  ! orbital and spin resonances to be left in the map
     type(real_8) n0(3)     ! n0 vector
     type(real_8) theta0    !  angle for the matrix around the orbit (analogous to linear tunes)
!!!Envelope radiation stuff
     real(dp) s_ij0(6,6)  !  equilibrium beam sizes
     ! equilibrium emittances (partially well defined only for infinitesimal damping)
     real(dp) emittance(3),tune(3),damping(3)
     logical(lp) AUTO,STOCHASTIC
     real(dp)  KICK(3)   ! fake kicks for tracking stochastically
     real(dp)  STOCH(6,6)  ! Diagonalized of stochastic part of map for tracking stochastically
  end type normal_spin
\end{verbatim}
}
If NS is of type normal\_spin and DS is a damapspin, then DS can be normalised as follows:
\begin{verbatim}
      NS=DS
\end{verbatim}
in complete analogy with the orbital normal form. In fact the analogy  on the computer  is  complete. \footnote{The fact that the spin is a spectator breaks the analogy slightly--- we can normalise  the orbital part first and the spin part later. } 

 
\sk
So, as I said, if you believe in the normal form of \eq{normalspin}, then even a stupid person can  comprehend the concept of the invariant spin axis in the normalised space. 

In the normalised space,  we notice that the spin vector $\vec{e}_2=(0,1,0)$ is an invariant of $R=(r,\exp(\theta L_y))$. Of course the choice of $e_2$ for the spin normal form is arbitrary. In the rarefied air of mathematics, we are free to use anything for the normal form axis. We chose a ``vertical'' axis.  Furthermore the angle of the spin rotation  $\theta$ is itself invariant because it depends only on the invariant actions  of the orbital map $r$.
So the spin axis is defined.

We now intend to show the obvious: the vector $A \vec{e}_{2}$ is the spin field axis $\vec{n}$.

\section{\color{.} Factoring the Map $T$ and the invariant spin axis $\vec{n}$}

The map $U$ can be conveniently factorized as follows:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*,^"!*|:!U,]<c!$1|
%|(#I,LA}}""Symbol^:"&c0  .P<c!$1(#$^:!&c0!*a^1_,LI}}:"&c0  .P<c!$1|
%|(#$^:!&c0!*a^'b!L_,LI}}:"&c0  .P<c!$1(#$^:!&c0!*a("n'b!L}_,LI}}|
%|: ;8/<:!;bP8asfac: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}U=
\left({I,A}\right)\circ \left({{a}_{1},I}\right)\circ \left({{a}_{\ell},I}\right)\circ \left({{a}_{n\ell},I}\right)\label{asfac}\end{eqnarray}
%
%
The total map $T$ is thus given  as
%
%]|Expr|[#b @`b___})># b'4" Chicago^: ;bP8&c0!*,^"!*|:!T""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" <c!$1(#:!m,LS}}:" : ;8/<:!;bP8normalspinf|
%|: ;8/=:#;bP8-<-<:" :# :" :#,F:!,]:#,F:" <c!$1(#:!I,LA}}:"&c0  .P|
%|<c!$1(#$^:!&c0!*a^1_,LI}}:"&c0  .P<c%"C)!# b'4<cY(!('<c%"C^<cY(!|
%|(#<c!$1(#$^:!&c0!*a^'b!L_,LI}}:"&c0  .P<c!$1(#$^:!&c0!*a("n'b!L}|
%|_,LI}}}}_)*# b(<:#-<mbox-[-<normalsize ,D<c!$1(#$^:!a^f_,LI}}|
%|:#,D-]}# b D b!( b!L!WW}}:"&c0  .P<c!$1(#:!&c0!*r,L$^e_(":"&c0  q|
%|$^:!&c0!*L^y_}}}:"&c0  .P<c!$1(#$^:!&c0!*a("n'b!L}(",M1},LI}}|
%|:"&c0  .P<c!$1(#$^:!&c0!*a^'b!L(",M1},LI}}}}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_)*# b'4:#-<mbox-[-<normalsize |
%|,D<c!$1(#$^:!m^f_,L$^e_($:"&c0  q.P$^:!&c0!*a^f(",M1}$^L^y_}}}|
%|:#,D-]}& b!( b"0 b#8 b$@ b%H b&P!WW}}:"&c0  .P<c!$1(#$^:!&c0!*a|
%|^1(",M1},LI}}:"&c0  .P<c!$1(#:!&c0!*I,L$^A_(",M1}}}: .O,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}T &=
& \left({m,S}\right) \label{normalspinf}\\   &=
& \left({I,A}\right)\circ \left({{a}_{1},I}\right)\circ \overbrace{\overbrace{\left({{a}_{\ell},I}\right)\circ \left({{a}_{n\ell},I}\right)}\limits_{}^{\mbox{\normalsize $\left({{a}_{f},I}\right)$}}\circ \left({r,{e}^{\theta {L}_{y}}}\right)\circ \left({{a}_{n\ell}^{-1},I}\right)\circ \left({{a}_{\ell}^{-1},I}\right)}\limits_{}^{\mbox{\normalsize $\left({{m}_{f},{e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}}\right)$}}\circ \left({{a}_{1}^{-1},I}\right)\circ \left({I,{A}^{-1}}\right)\nonumber 
\end{eqnarray}
%



First, let us ignore the transformation %
%]|Expr|[#b @`b___})## b'4" Helvetica|: ;bP8&c0!*,D<c!$1(#$^"!*|:!a|
%|^1_,LI}}: ,D}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
$\left({{a}_{1},I}\right)$;
this transformation brings us to a parameter dependent system, i.e., it does not tell us how the system is but how the system would be if we changed some parameters, for example quadrupole strengths. 
Therefore let us look at the map:
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!T^f_""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" "$Monaco^:$ <c!$1(#$^:!m^f_,L$^S^f|
%|_}}: ;bP;/":#;bP8 :" :#,F:!,]:#,F:" <c%"C^<cY A)%# b'4<c!$1(#|
%|:!I,LA}}:"&c0  .P<c%"C^<cY(!('<c%"C^<cY(!(#<c!$1(#$^:!&c0!*a^|
%|'b!L_,LI}}:"&c0  .P<c!$1(#$^:!&c0!*a("n'b!L}_,LI}}}}_)*# b(<:#-<|
%|mbox-[-<normalsize ,D<c!$1(#$^:!a^f_,LI}}:#,D-]|
%|}# b D b!( b!L!WW}}:"&c0  .P<c!$1(#:!&c0!*r,L$^e_(":"&c0  q$^|
%|:!&c0!*L^y_}}}:"&c0  .P<c!$1(#$^:!&c0!*a("n'b!L}(",M1},LI}}:"&c0  .P|
%|<c!$1(#$^:!&c0!*a^'b!L(",M1},LI}}}}_)*# b'4:#-<mbox-[-<normalsize|
%| ,D<c!$1(#$^:!m^f_,L$^e_($:"&c0  q.P$^:!&c0!*a^f(",M1}$^L^y_}}}|
%|:#,D-]}& b!( b"0 b#8 b$@ b%H b&P!WW}}:"&c0  .P<c!$1(#:!&c0!*I|
%|,L$^A_(",M1}}}}& b!( b"0 b#8 b$@ b%H b&P!WW}}):# b'4:$Map around|
%| the parameter dependent fixed point:#-<mbox-[-<normalsize ,D|
%|:$ ,] <c!$1(#$^:!m^f_,L$^S^f_}}:#,D-]}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|_}: ;8/<:!;bP8normalspimoan1: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{T}_{f} &=
& ~\left({{m}_{f},{S}_{f}}\right)\nonumber \\
  &=
& \underbrace{\left({I,A}\right)\circ \overbrace{\overbrace{\left({{a}_{\ell},I}\right)\circ \left({{a}_{n\ell},I}\right)}\limits_{}^{\mbox{\normalsize $\left({{a}_{f},I}\right)$}}\circ \left({r,{e}^{\theta {L}_{y}}}\right)\circ \left({{a}_{n\ell}^{-1},I}\right)\circ \left({{a}_{\ell}^{-1},I}\right)}\limits_{}^{\mbox{\normalsize $\left({{m}_{f},{e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}}\right)$}}\circ \left({I,{A}^{-1}}\right)}\limits_{{\rm M}{\rm a}{\rm p}~{\rm a}{\rm r}{\rm o}{\rm u}{\rm n}{\rm d} ~{\rm t}{\rm h}{\rm e}~{\rm p}{\rm a}{\rm r}{\rm a}{\rm m}{\rm e}{\rm t}{\rm e}{\rm r}~{\rm d} {\rm e}{\rm p}{\rm e}{\rm n}{\rm d} {\rm e}{\rm n}{\rm t}~{\rm f}{\rm i}{\rm x}{\rm e}{\rm d} ~{\rm p}{\rm o}{\rm i}{\rm n}{\rm t}\mbox{\normalsize $~=~\left({{m}_{f},{S}_{f}}\right)$}}^{}\label{normalspimoan1}\end{eqnarray}
%
This map is truly the map used by the tracking code. We will consider the following map 
%
%]|Expr|[#b @`b___})%# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!T^f_""Symbol^:"&c0  .P|
%|<c!$1(#:!&c0!*I,LA}}: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{T}_{f}\circ \left({I,A}\right)\end{eqnarray}
%
computed two different ways. First, we use the obvious way:
%
%]|Expr|[#b @`b___})9# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!T^f_""Symbol^:"&c0  .P|
%|<c!$1(#:!&c0!*I,LA}}:" "#Helvetica|:#,F:!,]:#,F:" <c!$1(#$^:!m|
%|^f_,L$^S^f_}}:"&c0  .P<c!$1(#:!&c0!*I,LA}}: ;bP;/":#;bP8 :" :#,F|
%|:!,]:#,F:"  <c!$1($$^:!m^f_,L$^S^f_A}}: ;8/<:!;bP8bmttrack: ;8/=|
%|;bP8,\, }& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{T}_{f}\circ \left({I,A}\right) &=
& \left({{m}_{f},{S}_{f}}\right)\circ \left({I,A}\right)\nonumber \\
  &=
&  \left({{m}_{f},{S}_{f}A}\right)\label{bmttrack}\end{eqnarray}
%?
Now, let us use the normal form representation of \eq{normalspimoan1} and rewrite \eq{bmttrack}:
%
%]|Expr|[#b @`b___})b F# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!T^f|
%|_""Symbol^:"&c0  .P<c!$1(#:!&c0!*I,LA}}:" "#Helvetica|:#,F:!,]|
%|:#,F:" <c!$1(#:!I,LA}}:"&c0  .P<c!$1(#$^:!&c0!*m^f_,L$^e_($:"&c0  q|
%|.P$^:!&c0!*a^f(",M1}$^L^y_}}}:"&c0  .P<c!$1(#:!&c0!*I,L$^A_("|
%|,M1}}}:"&c0  .P<c!$1(#:!&c0!*I,LA}}: ;bP;/":#;bP8 :" :#,F:!,]|
%|:#,F <c!$1(#:!I,LA}}:"&c0  .P<c!$1(#$^:!&c0!*m^f_,L$^e_($:"&c0  q|
%|.P$^:!&c0!*a^f(",M1}$^L^y_}}}: ;bP;/":#;bP8 ,F:!,]:#,F <c!$1(&|
%|$^:!m^f_,LA:"&c0  .P$^:!&c0!*m^f_$^e_($:"&c0  q.P$^:!&c0!*a^f|
%|(",M1}$^L^y_}}}: ;8/<:#nf;bP8track: ;8/=;bP8,\, |
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{T}_{f}\circ \left({I,A}\right) &=
& \left({I,A}\right)\circ \left({{m}_{f},{e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}}\right)\circ \left({I,{A}^{-1}}\right)\circ \left({I,A}\right)\nonumber \\
  &=
& \left({I,A}\right)\circ \left({{m}_{f},{e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}}\right)\nonumber \\
 &=
& \left({{m}_{f},A\circ {m}_{f}{e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}}\right)\label{nftrack}\end{eqnarray}
%?
Comparing \eqs{bmttrack} and \eqe{nftrack}, we conclude that:
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!S^f_A|
%|""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" :!A:"&c0  .P$^:!&c0!*m|
%|^f_ $^e_($:"&c0  q.P$^:!&c0!*a^f(",M1}$^L^y_}: ;8/<:!;bP8eqdefn|
%|: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{S}_{f}A &=
& A\circ {m}_{f}\ {e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}\label{eqdefn}\end{eqnarray}
%

We now apply \eq{eqdefn}, on the vector ${\vec e}_2$ using the Einstein summation convention on repeated indices:
%
%]|Expr|[#b @`b___})b L# b'4" Chicago^: ;bP8&c0!*,^$^<c!!!(#$^|
%|"!*|:!S^f_A$)"# b'4<c$%!^e}}& b!( b"0 b#8 b$@ b%H b&P!WW}^2_}}|
%|^a_""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" $^<c!!!(&:!A:"&c0  .P|
%|$^:!&c0!*m^f_ $^e_($:"&c0  q.P$^:!&c0!*a^f(",M1}$^L^y_}$)!# b'4|
%|<c$%!^e}}& b!( b"0 b#8 b$@ b%H b&P!WW}^2_}}^a_: ;bP;/":#;bP8 |
%|$^:!S(#f,[ab}_$^A("b2}_:#,F:!,]:#,F$^:!A("a2}_:"&c0  .P$^:!&c0!*m|
%|^f_: ;bP;/":#;bP8 "$Monaco^:$because"%Times|:% $^:!e_($:"&c0  q|
%|.P$^:!&c0!*a^f(",M1}$^L^y_}$)!# b'4<c$%!^e}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}^2_:" :#,F:!,]:#,F:" $)!# b'4<c$%!|
%|^:!e}}& b!( b"0 b#8 b$@ b%H b&P!WW}^2_ :$i,Ne,N,L normal form|
%|,A: ;8/<:!;bP8eqdefc: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{\left.{{S}_{f}A{\vec{e}}_{2}}\right|}_{a} &=
& {\left.{A\circ {m}_{f}\ {e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}{\vec{e}}_{2}}\right|}_{a}\nonumber \\
 {S}_{f;ab}{A}_{b2}&=
&{A}_{a2}\circ {m}_{f}\nonumber \\
 {\rm b}{\rm e}{\rm c}{\rm a}{\rm u}{\rm s}{\rm e}\ {e}^{\theta \circ {a}_{f}^{-1}{L}_{y}}{\vec{e}}_{2} &=
& {\vec{e}}_{2}\ {\rm i}.{\rm e}.,~{\rm n}{\rm o}{\rm r}{\rm m}{\rm a}{\rm l}~{\rm f}{\rm o}{\rm r}{\rm m}!\label{eqdefc}\end{eqnarray}
%
%
Therefore, we deduce from the \eq{eqdefc} that the vector $\vec n$ defined as $A{\vec e}_2$ transforms as follows:
%
%]|Expr|[#b @`b___})/# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!S^f_<c$%!|
%|^n}""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" <c$%!^:!n}:"&c0  .P|
%|$^:!&c0!*m^f_: ;8/<:!;bP8eqdef: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{S}_{f}\vec{n} &=
& \vec{n}\circ {m}_{f}\label{eqdef}\end{eqnarray}
%
Equation \eqe{eqdef} indicates that $\vec n$  is truly an invariant vector function of phase space as well as being a solution of the spin motion. In other words it transforms under the action $S_f$ as an  orbital function would. 
Of course it was designed to be so by virtue of the assumed existence of the normal form.

\section{\color{.} A map with a single spin resonance}\label{secspinres}

The map $R$ could contain a single orbital resonance and additionally the spin part could have a single resonance as well. For example, the normalised spin part can be:
%
%]|Expr|[#b @`b___})b C# b'4" Chicago^: ;bP8&c0!*,^"!Helvetica|:! |
%|$^""Times|:"S^r_:!,F:",]:!,F"#Symbol^:# :!-<exp<c!$1)"# b'4$^|
%|"$*|:$b^y_$^L^y_}& b!( b"0 b#8 b$@ b%H b&P!WW}}:!-<exp<c!$1)%# b'4|
%|:#&c0  bL:$&c0!*,K<c$%A^:#&c0  b}:$&c0!* <c$%A^:#&c0  L}|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}}: ;bP;&c0!*/" :#;bP8 :!,F:#;bP;&c0  />|
%|:!;bP8&c0!*,F:# $^:$b^x_$^L^x_,K$^b^z_$^L^z_,]:#&c0  bL:$&c0!*,K|
%|<c$%A^:#&c0  b}:$&c0!* <c$%A^:#&c0  L}: ;8&c0!*/<:$;bP8onespinres|
%|: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray} {S}_{r}&=
& \exp\left({{b}_{y}{L}_{y}}\right)\exp\left({\beta \Lambda +\overline{\beta }\ \overline{\Lambda }}\right)\nonumber \\
  &\Rightarrow & {b}_{x}{L}_{x}+{b}_{z}{L}_{z}=
\beta \Lambda +\overline{\beta }\ \overline{\Lambda }\label{onespinres}\end{eqnarray}
%
%
This equation contains  eigenphasors  of the normalised angular momentum operator $L_y$:
%
%]|Expr|[#b @`b___})b T# b'4" Chicago^: ;bP8&c0!*,^"!Symbol^:!&c0  L|
%|&c0!*  ""Helvetica|:",F"#*|:#,]:",F:!  <2^:#1^2}<c!(A($$^L^x_|
%|,Ki$^L^z_}} "$Monaco^:$and:# <c$%A^:!&c0  L}&c0!* :#,]:! <2^:#1|
%|^2}<c!(A($$^L^x_,Mi$^L^z_}}: ;bP;/":";bP8 ad$^:#L^y_:!&c0  L&c0!* |
%|:",F:#,]:",F:! <c!=Q)$# b'4$^:#L^y_,L<2^1^2}<c!(A($$^L^x_,Ki$|
%|^L^z_}}}& b!( b"0 b#8 b$@ b%H b&P!WW}},]i:!&c0  L: ;bP;&c0!*/"|
%|:";bP8 :!&c0  />:#&c0!* :!&c0  b&c0!* :",F:#,]:",F:! $^:#b^x_|
%|,Mi$^b^z_: ;8/<:#;bP8resbas: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}\Lambda   &=
&  {1 \over 2}\left\{{{L}_{x}+i{L}_{z}}\right\}\ {\rm a}{\rm n}{\rm d} \ \overline{\Lambda } =
 {1 \over 2}\left\{{{L}_{x}-i{L}_{z}}\right\}\nonumber \\
 ad{L}_{y}\Lambda  &=
& \left[{{L}_{y},{1 \over 2}\left\{{{L}_{x}+i{L}_{z}}\right\}}\right]=
i\Lambda \nonumber \\
 \Rightarrow \ \beta  &=
& {b}_{x}-i{b}_{z}\label{resbas}\end{eqnarray}
%
Therefore if the function $\beta$ in \eq{onespinres} contains phasors of a single orbital resonance, say $3Q_x$, then this particular operator corresponds to the $3Q_x+{\nu}_{spin}=k$ resonance. Then it is a matter of pattern recognition and mimicry, to conclude that the Froissart-Stora formula is given by:
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*,^<2^$^"!*|:!P^|
%|f_^$^P^0_}""Symbol^:" "#Helvetica|:#,F:!,]:#,F:" :!2:#-<exp<c!$1|
%|(":!,M<2^:"&c0  e(#:!&c0!*4<2("d:"&c0  n}(!:!&c0!*dn}}}}}},M1|
%|: ,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{{P}_{f} \over {P}_{0}} &=
& 2\exp\left({-{\varepsilon  \over 4{d\nu  \over dn}}}\right)-1\end{eqnarray}
%
This formula depends on the rate of change of tune $d\nu  \over dn$ over one turn and on a resonant strength $\varepsilon $ which is given by:
%
%]|Expr|[#b @`b___})<# b'4" Chicago^: ;bP8&c0!*,^"!Symbol^:!&c0  e|
%|""*|:"&c0!*,]$^<c!!!(#<c!$1(":!&c0  b<c$%A^b}}}.P$^:"&c0!*a_("|
%|,M1}}}(#z,]$^z^0_}_"#Monaco^:#  and in fact,L per Barber,L :!&c0  b|
%|:"&c0!* :!&c0  .Z:"&c0!* <2^$(""$Helvetica|:$-<varepsilon}_(!|
%|:#sb}(":"2:!&c0  p}}:"&c0!* : ;8/<:";bP8res: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}\varepsilon =
{\left.{\left({\beta \overline{\beta }}\right)\circ {a}^{-1}}\right|}_{z=
{z}_{0}}~~{\rm a}{\rm n}{\rm d} ~{\rm i}{\rm n}~{\rm f}{\rm a}{\rm c}{\rm t},~{\rm p}{\rm e}{\rm r}~{\rm B}{\rm a}{\rm r}{\rm b}{\rm e}{\rm r},~\beta \ \equiv \ {{\varepsilon}^{{\rm s}{\rm b}} \over 2\pi }\ \label{res}\end{eqnarray}



\chapter{\color{.} A  Complex Package}\label{chapcp}

It is possible to complexify the original package of Berz and to use it together with the real package, i.e., with FPP. This appendix shows examples of PTC where the real and complex package are used to perform the same calculation. The big difference is that the complex package provides a normal form algorithm which, like the tiny package of appendix \ref{apptiny}, is ``understandable.''

In future codes, one might imagine fast versions of a real package used to perform the regular TPSA around the ring coupled with a single complex analysis package. This seems to me to be the optimal solution.

\section{\color{.}  Presently implemented tools }\label{seccomplextools}
The tools and types presently supported are all found in the files h_definition.f90 and  Ci_tpsa.f90. This file is  grossly speaking the complex equivalent of the file i_tpsa.f90 but also contains all the analysis tools.  

At present, the structures used are:

\begin{verbatim}
  TYPE C_taylor
     INTEGER I !@1  integer I is a pointer to the complexified Berz package
  END TYPE C_taylor

  TYPE c_DAMAP
     TYPE (c_TAYLOR) V(LNV) 
     type(c_spinmatrix) s
     complex(dp) e_ij(6,6)
     integer :: N=0
  END TYPE c_DAMAP

  TYPE c_vector_field
      integer :: n=0,nrmax
      real(dp) eps    
      type (c_taylor) v(lnv)                        
  END TYPE c_vector_field

  TYPE c_factored_lie
      integer :: n= 0   
      integer :: dir= 0     
       type (c_vector_field), allocatable :: f(:)                   
  END TYPE c_factored_lie

  TYPE c_normal_form
      type(c_damap) a1
      type(c_damap) a2
      type(c_factored_lie) g   ! nonlinear part of a in phasors
      type(c_factored_lie) ker !  kernel i.e. normal form in phasors
      type(c_damap) a_t ! transformation a (m=a n a^-1) 
      type(c_damap) n   ! transformation n (m=a n a^-1)      
      type(c_damap) As  !  For Spin   (m = As a n a^-1 As^-1)  
      integer NRES,M(NDIM2t/2,NRESO),ms(NDIM2t/2)
      real(dp) tune(NDIM2t/2),damping(NDIM2t/2),spin_tune
!!!Envelope radiation stuff to normalise radiation (Sand's like theory)
     complex(dp) s_ij0(6,6)  !  equilibrium beam sizes
     complex(dp) s_ijr(6,6)  !  equilibrium beam sizes in resonance basis
     real(dp) emittance(3)
  END TYPE c_normal_form

\end{verbatim}

The type c_vector_field is a complex vector field. It can be acting on a c_taylor or on a c_damap following the rules in the online documentation of the real FPP. For example, 
%
%]|Expr|[#b @`b___})-# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"M,]:!-<exp<c!$1(#<c$%!^:"F}"#Symbol^:#&c0  /7<c$%!^/1}}}|
%|:"&c0!*A:! : ;8/<:!;bP8vecexpdef: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} M=
\exp\left({\vec{F}\cdot \vec{\nabla }}\right)A \label{vecexpdef}\end{align}
where $M$ and $A$ are Taylor maps (c_taylor) can be realised with the syntax:
\begin{verbatim}
    M=exp(F,A)
\end{verbatim}
Moreover, I also permit in the complex FPP the Dragt abuse of notation, namely
\begin{verbatim}
    M=exp(F)
!!! is equivalent to
    M=exp(F,Identity)
\end{verbatim}
The Dragt abuse of notation has been a source of confusion for many serious users of Lie methods, including his students,  since it confuses the standard phase space map acting on real numbers with the map acting on functions of real numbers. In perturbation theory  we try to modify functions: Hamiltonians and vector functions of vector fields.



A  c_factored_lie representation denoted by ``h'' is a product of ``n'' exponentiated vector fields ``f(i)''. The integer ``dir'' is either ``1'' (Dragt-Finn product) or ``-1'' for reverse Dragt-Finn. 
In Dragt's work, the index ``i'' usual labels the degree of some homogeneous polynomial. Here it can be anything. In the normal form as implemented, it has indeed this particular meaning.
But  it can have other more useful meanings: for example it may be ordered as a function of the orbital variables. This is a very useful factorization. In any event, any factored map can be redefined later into a more commodious form.

Here is the code that exponentiates the c_factored_lie representation:
\begin{verbatim}
    if(h%dir==1) then
      do i=h%n,1,-1
       c_expflo_fac=texp(h%f(i),c_expflo_fac)
      enddo   
    else
      do i=1,h%n
       c_expflo_fac=texp(h%f(i),c_expflo_fac)
      enddo   
    endif
\end{verbatim}

The inverse of an c_factored_lie representation ``h'' involves more than just a minus sign in front of the vector field: the order must also be reversed. Therefore the following functions is defined for the inverse:
\begin{verbatim}
    M=exp_inv(h,A)
\end{verbatim}
which is equivalent to
\begin{verbatim}
    h%dir=-h%dir
    M=exp(-h,A)
\end{verbatim}

Finally a few operators had to be redefined to avoid confusion with the real package. For example, the documented operator .mono. that produces real monomial in the original FPP is now .cmono.
This was necessary to avoid a Fortran90 confusion.  The reader is invited to look at the preamble of the module c_tpsa in Ci_tpsa.f90 to see what is new or redefined. One useful addition is the definition of a complex logarithm which facilitates the extraction of tunes and damping decrements.

\section{\color{.}  Phasors Basis of this package}\label{seccp}

The best way to illustrate  that the package is complex is to invoke the function ``from_phasor'' and ``to_phasor''. The code \htm{tunes_c.f90} is used. The phasors are printed in the file ``phasors.txt.''
We run the simplest PTC case; the reader is invited to run the most complex case (case 1 with RF-modulation of magnet).

{\footnotesize
\begin{verbatim}
  small misalignments and rotations in lattice ? input t or f
f
  Map with  6d without cavity -> 1
  Map with only 4d + delta  -> 2
  Map with only 6d + cavity  -> 3
  Map with only 6d + radiation + cavity  -> 4
  Map with only 6d + radiation + cavity + stochasticity  -> 5
2
  Modulate the magnet Bend1 ? yes -> t, no -> f
f
  input an order > 0 (not too big first)!
1
  PARENT LAYOUT NAME :No name assigned
  NUMBER OF ORIGINAL LAYOUT ELEMENTS :         529
  NUMBER OF THIN OBJECTS :        2957
  TOTAL IDEAL LENGTH OF STRUCTURE :   196.83852061772484
  TOTAL INTEGRATION LENGTH OF STRUCTURE (mad8 style survey) :   196.83852061772484
 closed orbit =    0.0000       0.0000       0.0000       0.0000       0.0000       0.0000
  Spin Map is identity : not normalized
           1 -0.18196555711988802       0.98330490491101319
           2 -0.18196555711988802      -0.98330490491101319
           3 -1.48612365431174953E-002  0.99988956572634857
           4 -1.48612365431174953E-002 -0.99988956572634857
  The order of the planes has been guessed using the algorithm in c_locate_planes
  Hopefully it is correct! Please check!
 Order guessed ->  1  3

  hit return to terminate program

\end{verbatim}
}

Now, open the file phasors.txt which was produced by the code fragment

{\footnotesize
\begin{verbatim}
call kanalnummer(mfp,"phasors.txt")
 cmap=to_phasor()
 write(mfp,*) " to_phasor "
call print(cmap,mfp)
 cmap=from_phasor()   
 write(mfp,*) "  "
 write(mfp,*) " from_phasor "
call print(cmap,mfp)
close(mfp)
\end{verbatim}
}

The result is simply:

{\tiny
\begin{verbatim}
  to_phasor
   
           4  Dimensional map 

          1, NO =    1, NV =    5, INA =    6
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     1      NV =     5
   1  1.0000000000000000       0.000000000000000       1  0  0  0  0
   1   0.000000000000000      1.0000000000000000       0  1  0  0  0
    -2   0.000000000000000                           0   0  0  0  0

          1, NO =    1, NV =    5, INA =    7
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     1      NV =     5
   1  1.0000000000000000       0.000000000000000       1  0  0  0  0
   1   0.000000000000000     -1.0000000000000000       0  1  0  0  0
    -2   0.000000000000000                           0   0  0  0  0

          1, NO =    1, NV =    5, INA =    8
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     1      NV =     5
   1  1.0000000000000000       0.000000000000000       0  0  1  0  0
   1   0.000000000000000      1.0000000000000000       0  0  0  1  0
    -2   0.000000000000000                           0   0  0  0  0

          1, NO =    1, NV =    5, INA =    9
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     1      NV =     5
   1  1.0000000000000000       0.000000000000000       0  0  1  0  0
   1   0.000000000000000     -1.0000000000000000       0  0  0  1  0
    -2   0.000000000000000                           0   0  0  0  0
   
  from_phasor
   
           4  Dimensional map 

          1, NO =    1, NV =    5, INA =    6
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     1      NV =     5
   1  0.5000000000000000       0.000000000000000       1  0  0  0  0
   1  0.5000000000000000       0.000000000000000       0  1  0  0  0
    -2   0.000000000000000                           0   0  0  0  0

          1, NO =    1, NV =    5, INA =    7
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     1      NV =     5
   1   0.000000000000000     -0.5000000000000000       1  0  0  0  0
   1   0.000000000000000      0.5000000000000000       0  1  0  0  0
    -2   0.000000000000000                           0   0  0  0  0

          1, NO =    1, NV =    5, INA =    8
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     1      NV =     5
   1  0.5000000000000000       0.000000000000000       0  0  1  0  0
   1  0.5000000000000000       0.000000000000000       0  0  0  1  0
    -2   0.000000000000000                           0   0  0  0  0

          1, NO =    1, NV =    5, INA =    9
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     1      NV =     5
   1   0.000000000000000     -0.5000000000000000       0  0  1  0  0
   1   0.000000000000000      0.5000000000000000       0  0  0  1  0
    -2   0.000000000000000                           0   0  0  0  0

\end{verbatim}
}
The above result must be compared to \eqs{phasorave} and \eqe{fromto}. Analytically this is the numerical version of \eq{phasors}. As can been seen in the matrix expression for the change of basis, the phasors are not a complex canonical pair. Indeed the Poisson bracket of two phasors belonging to the same plane, is $-2i$ as shown in \eq{pbpha}. It is also the determinant of the second matrix in \eq{phasorave}. 

\section{\color{.}  Phasors Basis: why do I reject  symplectic phasors?}\label{seccpw}

Since we now have a complex package, would it be more pleasing to use a complex symplectic transformation instead of the above? In that case, the Poisson bracket would be one. More over the rules on changing the  Hamiltonian would be the usual substitution rule. So why my non-symplectic choice? 

The answer is connected to the tune shifts and the damping decrements. In the linear case, it is always true that the tune is a pure imaginary number and that the damping is a real number. If we choose the phasors as above, this results extends to the nonlinear tunes and the amplitude dependent damping: they are respectively polynomial with pure imaginary coefficients and real coefficients at all orders. This is not true if a symplectic phasor transformation is used. For example, let use the following transformation:
%
%]|Expr|[#b @`b___})b A# b'4" Chicago^: ;bP8&c0!*,^"!Symbol^:!&c0  z|
%|""*|:"&c0!*,]<2(#x,Kip}(#1,Mi}}    "#Monaco^:#and:"     <c$%A|
%|^:!&c0  z}:"&c0!*,]<2(#x,Mip}(#1,Mi}}     -;:!&c0  z:"&c0!*,L|
%|<c$%A^:!&c0  z}:"&c0!*-=,]1 : ;8/<:";bP8pbphas: ;8/=;bP8,\|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}\zeta =
{x+ip \over 1-i}\ \ \ \ {\rm a}{\rm n}{\rm d} \ \ \ \ \ \overline{\zeta }=
{x-ip \over 1-i}\ \ \ \ \ [\zeta ,\overline{\zeta }]=
1\ \label{pbphas}\end{eqnarray}
%
%
And let us substitute \eq{pbphas} into a simple Hamiltonian amplitude dependent tune shift:
%
%]|Expr|[#b @`b___})7# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"H:!,F,]"#Symbol^:#&c0  w<2(#$^:"&c0!*x_^2,K$^p_^2}^2},K|
%|:#&c0  D$^<c!$1(#$^:"&c0!*x_^2,K$^p_^2}}_^2:#&c0  .N:"&c0!* ,M|
%|i:#&c0  wz<c$%A^z}:"&c0!*,M4:#&c0  D$^<c!$1("z<c$%A^z}}}_^:"&c0!*2|
%|: ;8/<:";bP8hs: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} H&=\omega {{x}^{2}+{p}^{2} \over 2}+\Delta {\left({{x}^{2}+{p}^{2}}\right)}^{2}\rightarrow \ -i\omega \zeta \overline{\zeta }-4\Delta {\left({\zeta \overline{\zeta }}\right)}^{2}\label{hs}\end{align}
We can easily compute the change of $\zeta $ with respect to time by taking the {\it usual }Poisson bracket:
%
%]|Expr|[#b @`b___})8# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<2("""*|:"d"#Symbol^:#&c0  z}(!:"&c0!*dt}}:!,F,]<c!=Q(#:#&c0  z|
%|:!&c0!*,LH}},] ,Mi:#&c0  wz:"&c0!*,M8:#&c0  D<c!$1("z<c$%A^z}}}|
%|z:"&c0!*,]<c%"C^<cY A^<c!(A(':!,Mi:#&c0  w:"&c0!*,M8:#&c0  D<c!$1|
%|("z<c$%A^z}}}}}}(!"$Monaco^:$&c0!*tune}_}:" :#&c0  z:!&c0!* : ;8/<|
%|:";bP8phst: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {d\zeta  \over dt}&=\left[{\zeta ,H}\right]= -i\omega \zeta -8\Delta \left({\zeta \overline{\zeta }}\right)\zeta =
\underbrace{\left\{{-i\omega -8\Delta \left({\zeta \overline{\zeta }}\right)}\right\}}\limits_{{\rm t}{\rm u}{\rm n}{\rm e}}^{}\ \zeta  \label{phst}\end{align}
\eq{phst}  displays a tune which has a imaginary linear tune and a ``real'' tune shift with amplitude. Let us do the same calculation with the non-symplectic phasors
%
%]|Expr|[#b @`b___})b F# b'4" Chicago^: ;bP8&c0!*,^"!*|:!h,]x,K|
%|ip    ""Monaco^:"and:!     <c$%A^h},]x,Mip     -;h,L<c$%A^h}-=,],M|
%|2i : ;8/<:!;bP8pbphash: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}h=
x+ip\ \ \ \ {\rm a}{\rm n}{\rm d} \ \ \ \ \ \overline{h}=
x-ip\ \ \ \ \ [h,\overline{h}]=
-2i\ \label{pbphash}\end{eqnarray}
%
and the result is
%
%]|Expr|[#b @`b___})5# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"H:!,F,]"#Symbol^:#&c0  w<2(#$^:"&c0!*x_^2,K$^p_^2}^2},K|
%|:#&c0  D$^<c!$1(#$^:"&c0!*x_^2,K$^p_^2}}_^2:#&c0  .N<2^w^:"&c0!*2}|
%|h<c$%A^h},K:#&c0  D$^<c!$1(":"&c0!*h<c$%A^h}}}_^2 : ;8/<:";bP8hns|
%|: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} H&=\omega {{x}^{2}+{p}^{2} \over 2}+\Delta {\left({{x}^{2}+{p}^{2}}\right)}^{2}\rightarrow {\omega  \over 2}h\overline{h}+\Delta {\left({h\overline{h}}\right)}^{2}\ \label{hns}\end{align}
and the tune is given by
%
%]|Expr|[#b @`b___})<# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|<2(!""*|:"dh}(!dt}}:!,F,],M2i<c!=Q(#h,LH}},] ,Mi"#Symbol^:#&c0  w|
%|:"&c0!*h,M4i:#&c0  D<c!$1(":"&c0!*h<c$%A^h}}}h,]<c%"C^<cY A^<c!(A|
%|((:!,Mi:#&c0  w:"&c0!*,M4i:#&c0  D<c!$1(":"&c0!*h<c$%A^h}}}}}}|
%|(!"$Monaco^:$tune}_}:" h:! : ;8/<:";bP8phnst: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {dh \over dt}&=-2i\left[{h,H}\right]= -i\omega h-4i\Delta \left({h\overline{h}}\right)h=
\underbrace{\left\{{-i\omega -4i\Delta \left({h\overline{h}}\right)}\right\}}\limits_{{\rm t}{\rm u}{\rm n}{\rm e}}^{}\ h \label{phnst}\end{align}
And we then substitute $h$ for $\zeta $ in \eq{phst}, we get:
%
%]|Expr|[#b @`b___})b A# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""Monaco^:"Tune:!,F,],Mi"#Symbol^:#&c0  w"$*|:$&c0!*,M8:#&c0  Dz|
%|<c$%A^z}:$&c0!* ,]:!,Mi:#&c0  w:$&c0!*,M8:#&c0  D<2(":$&c0!*h|
%|<c$%A^h}}^$^<c!$1(#1,Mi}}_^2},] ,Mi:#&c0  w:$&c0!*,M4i:#&c0  D|
%|:$&c0!*h<c$%A^h}:! : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\rm T}{\rm u}{\rm n}{\rm e}&=-i\omega -8\Delta \zeta \overline{\zeta }\ =
-i\omega -8\Delta {h\overline{h} \over {\left({1-i}\right)}^{2}}=
\ -i\omega -4i\Delta h\overline{h} \end{align}
So, as we can see, the result is the same but the use of the non-symplectic phasors facilitates the identification of the tunes and the damping: tunes are imaginary and damping decrements are real.
We must simply remember that if the Poisson bracket is used, it involves a factor of $-2i$. This is not a big deal especially within the context of FPP where the Poisson bracket is {\it never } used internally so as to accommodate non-symplectic maps and non-canonical variables.


\section{\color{.}Some examples of tune/damping calculation }\label{secexamx}

The complex normal form\footnote{Spin and stochastic envelopes now included} is at present given by:

\begin{verbatim}
  TYPE c_normal_form
      type(c_damap) a1
      type(c_damap) a2
      type(c_factored_lie) g   ! nonlinear part of a in phasors
      type(c_factored_lie) ker !  kernel i.e. normal form in phasors
      type(c_damap) a_t ! transformation a (m=a n a^-1) 
      type(c_damap) n   ! transformation n (m=a n a^-1)      
      type(c_damap) As  !  For Spin   (m = As a n a^-1 As^-1)  
      integer NRES,M(NDIM2t/2,NRESO),ms(NDIM2t/2)
      real(dp) tune(NDIM2t/2),damping(NDIM2t/2),spin_tune
!!!Envelope radiation stuff to normalise radiation (Sand's like theory)
     complex(dp) s_ij0(6,6)  !  equilibrium beam sizes
     complex(dp) s_ijr(6,6)  !  equilibrium beam sizes in resonance basis
     real(dp) emittance(3)
  END TYPE c_normal_form

\end{verbatim}

In the code   \htm{tunes_c.f90}, the map is first computed with our regular PTC tools. The map ``m'' gotten from PTC  is put into a complex map of type c_damap and then normalised using a new normal form:
\begin{verbatim}
  cmap=m

  call c_normal(cmap,cn)              
\end{verbatim}

The map is normalised  by c_normal and the results in put in cn of type c_normal_form. The normalised map vector field, in phasors, in stored in the Dragt-Finn factorization cn\%ker of type c_factored_lie:
\begin{verbatim}
  TYPE c_factored_lie
      integer :: n= 0   
      integer :: dir= 0     
      type (c_vector_field), allocatable :: f(:)                   
  END TYPE c_factored_lie
\end{verbatim}
Therefore we can read the tunes, damping decrements and momentum compaction directly from cn\%ker. The results are in file ``results.txt.'' 

At the end of ``results.txt'', I also compute the average of $<x^2>$. The first term of this calculation is half the beta function which can also be computed from the linear part of cn\%a_t 
using \eq{x2pa}. This is in the piece of code:
\begin{verbatim}
write(mf,*) "  "
write(mf,*) " Half the horizontal beta functions using the matrix A "
write(mf,*) "  "

beta=(cn%a_t%v(1).sub.'1')**2+(cn%a_t%v(1).sub.'01')**2

write(mf,*) " Half the beta = (A_11)^2+(A_12)^2 = ", beta/2
\end{verbatim}

The true average calculation is done with  a call to the  subroutine AVERAGE of the real FPP,

\begin{verbatim}
x2r=1.0_dp.mono.'2'  ! Creates x^2

call AVERAGE(x2r,normal%n%a_t,x2r)

write(mf,*) "  "
write(mf,*) " Average of x^2 using standard FPP "
write(mf,*) "  "
call print(x2r,mf)

\end{verbatim}
and, with a call to the  routine C_AVERAGE of the complex FPP,
\begin{verbatim}
x2=1.0_dp.cmono.'2' ! Creates x^2

write(mf,*) "  "
write(mf,*) " Average of x^2 using Complex FPP "
write(mf,*) "  "
call  C_AVERAGE(x2,cn%a_t,x2) 

call print(x2,mf)
\end{verbatim}

The reader is strongly encouraged to look at the function C_AVERAGE of the complex package, AVERAGE of the real FPP (in o_tree_element.f90) and the function AVERAGE in y_my_own_analysis.f90 of the tiny pedagogical package. Of course C_AVERAGE now looks very much like AVERAGE of the  pedagogical package. It was not called pedagogical without a good reason!

\section{\color{.}Stochastic beam envelopes: synchrotron Integrals }\label{secenvelope}

The map, linear in our case, can self-consistently propagate moments of a distribution. In the case of the complex FPP, the total one-turn stochastic linear map acts as follows on quadratic moments:
%
%]|Expr|[#b @`b___})7# b'4" Chicago^: ;bP8&c0!*.["!Helvetica|:! |
%|$^""Symbol^:"&c0  S_^"#*|:#&c0!*1:!,F,]M$^:"&c0  S_^:#&c0!*0$|
%|^:!M_^: t:#,KE:! : ;8/<:#;bP8mommapc: ;8/=:";bP8 :!-<-<:" $^&c0  S|
%|(!:#&c0!*ij}_:!,F,]<c!,Q("$^z^i_$^z^j_}}: ;8/<:!;bP8momdef: ;8/=|
%|;bP8.K}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{subequations}\begin{align} {\Sigma}^{1}&=M{\Sigma}^{0}{M}^{{\rm T}}+E \label{mommapc} \\ {\Sigma}_{ij}&=\left\langle{{z}_{i}{z}_{j}}\right\rangle\label{momdef}\end{align}\end{subequations}
The matrix $E_{ij}$ contains the sum total of all stochastic kicks around a ring; it is stored in the complex array $E_{ij}(6,6)$ of the c_damap.

The equilibrium beam sizes are obtained by solving the equation 
%
%]|Expr|[#b @`b___}).# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  S_^.E:!&c0!*,F,]M$^:"&c0  S_^.E$^:!&c0!*M_^|
%|: t"#*|:#,KE:! : ;8/<:#;bP8equil: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\Sigma}^{\infty }&=M{\Sigma}^{\infty }{M}^{{\rm T}}+E \label{equil}\end{align}

This is best done by going into the  phasors basis first. This is done in the normal form algorithm and it is remarkably simple. First we look to see if there are stochastic moments in the map
\begin{verbatim}
     call c_check_rad(m1%e_ij,rad_in)
     if(rad_in) call c_normal_radiation(m1,n)
\end{verbatim}
and then we call the normal form c_normal_radiation. Here {\tt m1} is the map to normalize and  {\tt n} is of type c_normal_form
Let us look at the subroutine  c_normal_radiation:

\begin{verbatim}
 subroutine c_normal_radiation(m1,n)
    implicit none
    type(c_damap) , intent(inout) :: m1
    type(c_normal_form), intent(inout) ::  n
    complex(dp) r(6,6)
    integer i,j

    r=m1

    do i=1,6 ; do j=1,6;
1)   n%s_ijr(i,j)= 1.0_dp/(1.d0- r(i,i)*r(j,j))*m1%e_ij(i,j)
    enddo ;enddo;

     do i=1,3
2)    n%emittance(i)=abs(n%s_ijr(2*i-1,2*i))/2.0_dp
     enddo

3) m1%e_ij= n%s_ijr  !using m1 to transform equilibrium beam sizes
4) m1=c_simil(n%a_t,c_simil(from_phasor(),m1,1),1)    ! c_simil(A,M,1) = A o M o A^-1   
   
5) n%s_ij0=m1%e_ij

   end  subroutine  c_normal_radiation 
\end{verbatim}

{\color{red} \bf N.B. The similarity transformation is done with c_simil rather than with a pure multiplication. This is due to the peculiar nature of the envelope E which must require special treatment when it is  part of a magnet map versus a change of coordinates.
}

I have labelled the lines. But here is the thing: the entire theory of synchrotron integrals is reduced to 4 trivial lines and one extra line to store the result. 
Here is the explanation:
%
\begin{enumerate}
\item The normalised map {\tt m1} enters in phasors basis so that it has  a diagonal matrix {\tt R}. \Eq{equil} being diagonal is now trivial to solve:
%
%]|Expr|[#b @`b___})4# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  S_^.E:!&c0!*,F,]R$^:"&c0  S_^.E$^:!&c0!*R_^|
%|: t"#*|:#,KE:! : ;bP;/":!;bP8 $^:"&c0  S_^.E:!&c0!*,F,]<2^1($|
%|1,M$^r(!ii}_$^r(!jj}_}}$^:#E(!ij}_: ;8/<:#;bP8equild: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\Sigma}^{\infty }&=R{\Sigma}^{\infty }{R}^{{\rm T}}+E \nonumber \\
 {\Sigma}^{\infty }&={1 \over 1-{r}_{ii}{r}_{jj}}{E}_{ij}\label{equild}\end{align}
%
\item  The so-called equilibrium emittances  are  actually 3 of the 21 equilibrium beam sizes  in phasors basis. They correspond to the fluctuation of the monomials made with a phasor with its complex conjugate.
We should add that the emittances are in fact  those computed by the Chao theory which is a theory that neglects the 18 remaining phasors from the very start. But there is no virtue, on a computer, to implement the Chao theory.  The Chao theory assumes small damping decrements. The Sands theory is even more approximate: it is without numerical virtues although it has some analytical virtues due to its simplicity. Sands and Chao have a deep connection related to the symplectic structure which remains valid with small damping decrements (see \cite{nishisands}).
\item The equilibrium beam sizes are copied in {\tt m1} for the benefit of the next line.
\item The beam sizes having been copied in {\tt m1}, they cannot be immediately transformed by the canonical transformation {\tt n\%a_t}. Since {\tt m1} is in complex basis, we must first put it in cartesian basis.
Next we apply   {\tt n\%a_t}.  
\item  The final result, the actual equilibrium beam sizes, are stored in the normal form of  type c_normal_form.
\end{enumerate}

The reader should run the program  \htm{tunes_c.f90} with the following input.

{\footnotesize 
\begin{verbatim}
  small misalignments and rotations in lattice ? input t or f
t
  Map with  6d without cavity -> 1
  Map with only 4d + delta  -> 2
  Map with only 6d + cavity  -> 3
  Map with only 6d + radiation + cavity  -> 4
  Map with only 6d + radiation + cavity + stochasticity  -> 5
5
  Order set to 1 for stochastic radiation calculation
  PARENT LAYOUT NAME :No name assigned
  NUMBER OF ORIGINAL LAYOUT ELEMENTS :         529
  NUMBER OF THIN OBJECTS :        2957
  TOTAL IDEAL LENGTH OF STRUCTURE :   196.83852061772484
  TOTAL INTEGRATION LENGTH OF STRUCTURE (mad8 style survey) :   196.83852061772484
 closed orbit =   0.33932E-03 -0.33139E-04  0.47219E-04  0.62445E-05  0.26844E-04 -0.86264E-02
  Spin Map is identity : not normalized
 New envelope calculation attempted with NO=1
           1 -0.16528182429916694       0.98620393353720071
           2 -0.16528182429916694      -0.98620393353720071
           3 -1.74473196689723664E-002  0.99981763436112769
           4 -1.74473196689723664E-002 -0.99981763436112769
           5  0.99885148041985172       4.68946091077993080E-002
           6  0.99885148041985172      -4.68946091077993080E-002
  The order of the planes has been guessed using the algorithm in c_locate_planes
  Hopefully it is correct! Please check!
 Order guessed ->  1  3  5
\end{verbatim}
}

The result is displayed in the file results.txt. The calculation is done with the real FPP (hard to follow) and with the complex FPP whose algorithm is simple.
The original map is computed by FPP and passed to both packages. The agreement is perfect.

\section{\color{.}  Spin calculation with the complex FPP}\label{seccspin}

Here we produce the calculation of \sec{secspinres}, namely \eq{onespinres}.  This is done in the code \htm{spin_res_c.f90}.

The first step consists in telling the code to leave one resonance in the map
and then to  separate the constant part of the spin from the nonlinear part
%
%]|Expr|[#b @`b___})2# b'4" Chicago^: ;bP8&c0!*,^"!Helvetica|:! |
%|$^""Times|:"S^r_:!,F:",]:!,F"#Symbol^:# :!-<exp<c!$1)"# b'4$^|
%|:#&c0  q^"$*|:$&c0!*0_$^L^y_}& b!( b"0 b#8 b$@ b%H b&P!WW}}:!-<|
%|exp<c!$1)(# b'4$^:$b^y_$^L^y_,K:#&c0  bL:$&c0!*,K<c$%A^:#&c0  b}|
%|:$&c0!* <c$%A^:#&c0  L}}& b!( b"0 b#8 b$@ b%H b&P!WW}}: ;8&c0!*/<|
%|:$;bP8spinres0: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray} {S}_{r}&=
& \exp\left({{\theta }_{0}{L}_{y}}\right)\exp\left({{b}_{y}{L}_{y}+\beta \Lambda +\overline{\beta }\ \overline{\Lambda }}\right)\label{spinres0}\end{eqnarray}
%
which is done with the code
\begin{verbatim}
c_spin0=1
C_SPIN0%S=c_map%S.SUB.0
C_SPIN0=C_SPIN0**(-1)*C_MAP
\end{verbatim}
On the third line, the spin map c_spin0 contains the factor $\exp\left({{b}_{y}{L}_{y}+\beta \Lambda +\overline{\beta }\ \overline{\Lambda }}\right)$
 of \eq{spinres0}.

It is then followed by two lines:

\begin{verbatim}
          call c_find_n0_da(c_spin0%s,C_N0)
          call c_n0_to_nr(C_N0,C_N0)
\end{verbatim}

The first subroutines computes the Lie exponent C_No of a purely ``nonlinear'' rotation, i.e., a rotation whose angle has no constant part.
The next step converts this exponent into the phasors basis of \eq{resbas}. 
It is useful to display this trivial routine:

\begin{verbatim}
  subroutine c_n0_to_nr(n0,nr)
    implicit none
    TYPE(c_spinor), INTENT(INout) :: n0,nr
    TYPE(c_spinor) nt

    call alloc(nt)

     nt%v(2)=n0%v(2)
     nt%v(1)=n0%v(1)-i_*n0%v(3) ! coefficient of  1/2(L_x + i L_z) 
     nt%v(3)=n0%v(1)+i_*n0%v(3) ! coefficient of  1/2(L_x - i L_z)
     nr=nt   

    call kill(nt)

  end subroutine c_n0_to_nr
\end{verbatim}

We could separate the tune shift term proportional to $L_y$; it would required a few extra lines. But this is not necessary in the present calculation since it is a low order calculation.
The entry C_N0\%V(2) commutes with the other components at the order of truncation and can be lumped with $\exp\left({{\theta }_{0}{L}_{y}}\right) $. 

Of course the inverse transformation exists and is equally trivial:

\begin{verbatim}
  subroutine c_nr_to_n0(nr,n0)
    implicit none
    TYPE(c_spinor), INTENT(INout) :: n0,nr
    TYPE(c_spinor) nt

    call alloc(nt)

     nt%v(2)=nr%v(2)
     nt%v(1)=(nr%v(1)+nr%v(3))/2.0_dp    ! coefficient of L_x
     nt%v(3)=i_*(nr%v(1)-nr%v(3))/2.0_dp ! coefficient of L_z
     n0=nt 

    call kill(nt)

  end subroutine c_nr_to_n0
\end{verbatim}

The reader should appreciate the ease of programming once a complex package exists.


\section{\color{.}  Stern-Gerlach Style Spin calculation with the complex FPP}\label{seccspinst}

The invariant spin field was explained to me by my spin mentor, Desmond Barber of DESY. 
I still remember his black board drawings: it was a physical explanation that took into account the spectator status of spin. Consequently, following his clear explanations, I viewed the spin normal form as a two-step process. 
\begin{enumerate}
\item First the orbital part is normalised. The orbital variables, at least the transverse ones, move on circles.
\item The matrix for the spin, an $SO(3)$ object in the case of PTC, is then normalized by an orbital dependent rotation: the resulting rotation is always around a given axis, the $y$-axis in FPP. 
\end{enumerate}
In normalized space, the properties of invariant spin field, as described to me by Desmond, are trivially deduced. Then the real invariant spin field is gotten by using the canonical transformation for the orbital and spin. 

The reader should appreciate that this process is not a sausage coming out of the meat grinder. Unlike the addition of a plane to treat the RF-modulated magnet, there is more to spin. In the case of spin, the additional ``plane'' is actually a $3 \times 3$ matrix. The normal form forces us to look at the Lie generator of that matrix, namely the angular momenta $L_x$, $L_y$ and $L_z$.

One can ask if there exists a normal form for the spin which amounts to an additional plane (symplectic or not) put into the meat grinder of the standard normal form. Actually if one contemplates the Stern-Gerlach effect, which destroys the spectator status of the spin, then the meat grinder   normal form becomes inevitable. For example, if Desmond had emphasized to me the fact that the spin, in some basis, is a planar rotation because the magnitude of the spin is conserved, then I might have immediately produced a normal form which is a simple extension of the orbital normal form.

I want to make it clear that I do not advocate a Stern-Gerlach type normal form: it is reasonable both physically and mathematically to take full advantage of the spectator status of the spin. However it is a nice example of the versatile features of a complex FPP to produce this alternate normal form with a few line of codes. This is the topic of this section.  


\subsection{\color{.}  Spin variables }\label{hamspinvar}
In a seminal paper\cite{yokoyas}, Yokoya explained the concept of the spin normal form in 1986.
In his paper, he wrote a Hamiltonian consisting of three spin variables defined as follows:
%
%]|Expr|[#b @`b___}):# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"s^1_:!,F,]<b R(#1,M$^J^s^2}_}-<cos<c!$1^$^"#Symbol^:#&c0  Y|
%|^:"&c0!*s_}: ;bP;/":!;bP8 $^:"s^3_:!,F,]<b R(#1,M$^J^s^2}_}-<|
%|sin<c!$1^$^:#&c0  Y^:"&c0!*s_}: ;bP;/":!;bP8 $^s^2_,F,]$^J^s_|
%| : ;8/<:";bP8spincano: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {s}_{1}&=\sqrt {1-{J}_{s}^{2}}\cos\left({{\Psi }_{s}}\right)\nonumber \\
 {s}_{3}&=\sqrt {1-{J}_{s}^{2}}\sin\left({{\Psi }_{s}}\right)\nonumber \\
 {s}_{2}&={J}_{s} \label{spincano}\end{align}
The action-angle variable $({\Psi }_s J_s)$ are assumed to be canonical. Yokoya showed the the full spin motion, including Stern-Gerlach effects, could be described by a Hamitonian
which uses the variables of \eq{spincano} in addition to the usual orbital variables.

If one accepts this statement, it is clear that the Hamiltonian can be normalised. If we neglect Stern-Gerlach effects, the final Hamiltonian, after normalisation, is simply:
%
%]|Expr|[#b @`b___})6# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""*|:"K<c!$1)'# b'4$^J^1_,N$^J^2_,L$^J^3_,L$^J^s_|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}},]$^K(!orbital}_<c!$1(%$^J^1_,N|
%|$^J^2_,L$^J^3_}},K"#Symbol^:#&c0  W:"&c0!*,H$^J^1_,N$^J^2_,L$|
%|^J^3_,I$^J^s_: ;8/<:";bP8hspin: ;8/=:!;bP8 : -;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} K\left({{J}_{1}.{J}_{2},{J}_{3},{J}_{s}}\right)=
{K}_{orbital}\left({{J}_{1}.{J}_{2},{J}_{3}}\right)+\Omega ({J}_{1}.{J}_{2},{J}_{3}){J}_{s}\label{hspin} \end{align}
Obviously, the spin frequency is simply $\Omega $; and the spin tune is the integral of  $\Omega $ around the ring. Done.

If one can add two extra ``canonical'' variables and produce a correct spin map, then it is conceivable that the algorithm for spin-orbit normalisation is that of the orbital with two additional dimensions added. In other words, spin in no more difficult than magnet modulation. 

This is certainly true of the analytical theory of Yokoya. However can Yokoya's calculation be performed on a Taylor type maps as produced by a TPSA package? The unfortunate answer  seems to be ``No''.  The reader will  notice that the canonical variables of \eq{spincano} do not have a polynomial dependence with the usual Floquet $(q_s,p_s)$. Indeed if we write 
%
%]|Expr|[#b @`b___});# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""*|:"q^s_,] <b R("2$^:!J^s_}_}-<cos<c!$1^$^"#Symbol^:#&c0  Y|
%|^:"&c0!*s_}"$Monaco^:$  and  $^:"p^s_,] ,M<b R("2$^:!J^s_}_}-<|
%|cos<c!$1^$^:#&c0  Y^:"&c0!*s_}:! : ;8/<:";bP8spinfloq: ;8/=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {q}_{s}=
\ \sqrt {2{J}_{s}}\cos\left({{\Psi }_{s}}\right)~~{\rm a}{\rm n}{\rm d} ~~{p}_{s}=
\ -\sqrt {2{J}_{s}}\cos\left({{\Psi }_{s}}\right) \label{spinfloq}\end{align}
Then the relationship between $\vec s$ and the $(q_s,p_s)$ is not a polynomial. This is unfortunate for a user of TPSA.

However if we are willing to abandon Hamiltonians, we can certainly write a map for the spin which involves two only variables. If the one-turn spin matrix is $S$, we can rewrite the map for $s_1$ and $s_3$ as follows:
%
%]|Expr|[#b @`b___})=# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^<c$%A^""*|:"s}^1_:!,F,]$^S(!11}_$^s^1_,K$^S(!13}_$^s^3_,K$^|
%|S(!12}_<b R)%# b'41,M$^s^1^2,M$^s^3^2}& b!( b"0 b#8 b$@ b%H b&P!WW}|
%|_}: ;bP;/":!;bP8 $^<c$%A^:"s}^3_:!,F,]$^S(!31}_$^s^1_,K$^S(!33}|
%|_$^s^3_,K$^S(!32}_<b R)%# b'41,M$^s^1^2,M$^s^3^2|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}_}: ;8/<:";bP8spinred: ;8/=:!;bP8 |
%|: -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\overline{s}}_{1}&={S}_{11}{s}_{1}+{S}_{13}{s}_{3}+{S}_{12}\sqrt {1-{s}_{1}^{2}-{s}_{3}^{2}}\nonumber \\
 {\overline{s}}_{3}&={S}_{31}{s}_{1}+{S}_{33}{s}_{3}+{S}_{32}\sqrt {1-{s}_{1}^{2}-{s}_{3}^{2}}\label{spinred} \end{align}
The variables  $s_1$ and $s_3$ can be considered to be our new additional canonical variables.
I would like to demonstrate the soundness (meat grinder wise) of the above statements.


\subsection{\color{.}  A meat grinder algorithm applied  for the spectator spin  }\label{hamspinvaralgo}

The example I will describe here is in  \htm{c_spin_st.f90}.


We first notice that  \eq{spinred} might have constant terms if ${S}_{12}$ or ${S}_{32}$ have a constant part. Therefore we will first transform the one-turn map by the  constant part of the spin transformation $(I,A_0)$ of \eq{normalspinf}. The resulting map
%
%]|Expr|[#b @`b___})1# b'4" Chicago^: ;bP8&c0!*,^$^"!*|:!T^1_""Symbol^:" |
%|"#Helvetica|:#,F:!,]:#,F:" <c!$1(#:!I,L$^A^0(",M1}}}:"&c0  .P|
%|<c!$1(#:!&c0!*m,LS}}:"&c0  .P<c!$1(#:!&c0!*I,L$^A^0_}}: ;8/<:!;bP8spint|
%|1: ;8/=;bP8,\}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{eqnarray}{T}_{1} &=
& \left({I,{A}_{0}^{-1}}\right)\circ \left({m,S}\right)\circ \left({I,{A}_{0}}\right)\label{spint1}\end{eqnarray}
%
will now be normalised two different ways. First we normalise it using the spectator techniques of section \sece{secspinfpp}  described in appendix \sece{appspin}.
This is done by the code
{\footnotesize
\begin{verbatim}
!!! Map normalised
NF_S=ID_S  ! Normalisation

a_t=NF_S%a_t
!! (a_t%m,a_t%s) = (I ,a_s%s) o (a_f%m, I ) o (a_l%m,I) o (a_nl%m,I)
call factor(a_t,a_s,a_f,a_l,a_nl)    
call fetch_s0(a_s,a_s)
ID_S=a_s**(-1)*ID_S*a_s  ! here a_s is only A_0

NF_S=ID_S   ! Normalisation

a_t=NF_S%a_t
!! (a_t%m,a_t%s) = (I ,a_s%s) o (a_f%m, I ) o (a_l%m,I) o (a_nl%m,I)
call factor(a_t,a_s,a_f,a_l,a_nl)   
 
n_axis=2    !  Creating e_y=(0,1,0)

n_axis=a_s*n_axis    ! ISF created
\end{verbatim}
}

Secondly, we normalised the extended map as described in \eq{spinred}:
{\footnotesize
\begin{verbatim}
  c_map=1  ! because of extra excess spin dimensions 
  c_map=saved_map

  C_SPIN0=0
1)  call c_normal_spin_linear(c_map,C_SPIN0,as,c_n0)

2)  call c_convert_spin(C_SPIN0,C_SPIN0)

3)  call c_normal(C_SPIN0,c_n,dospin=my_false)
\end{verbatim}
}

Line 1) removes $(I,A_0)$, as in \eq{spint1}, from the map as we did with the real FPP.  Line 2) creates \eq{spinred}. Finally the full map is normalised on line 3).


\subsection{\color{.}  The spin tune shift with amplitude  }\label{appspintune}

The result for the tune shift is put in file {\tt spin_invariant.txt}. The result agrees with the spectator normal form exactly:
{\footnotesize
\begin{verbatim}
 Leading order amplitude dependent spin tune
 using the spectator normal form

 etall    1, NO =    4, NV =    6, INA =  313
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     4      NV =     6
   2   31.06337985910767       2  0  0  0  0  0
   2   31.06337985910810       0  2  0  0  0  0
   2   488.2748054288063       0  0  2  0  0  0
   2   488.2748054288074       0  0  0  2  0  0
   2 -0.3077133960596272E-06   0  0  0  0  2  0
   2 -0.3077133960596310E-06   0  0  0  0  0  2
    -6   0.000000000000000       0  0  0  0  0  0
  
 Same terms computed with the extended phase space

          1, NO =    4, NV =    8, INA =   98
 *********************************************

    I  COEFFICIENT          ORDER   EXPONENTS
      NO =     4      NV =     8
   3   0.000000000000000       31.06337985911148       1  1  0  0  0  0  0  1
   3   0.000000000000000       488.2748054288129       0  0  1  1  0  0  0  1
   3   0.000000000000000     -0.3077133960596044E-06   0  0  0  0  1  1  0  1
    -3   0.000000000000000                           0   0  0  0  0  0  0  0
\end{verbatim}
}
The first result is in Cartesian Floquet variables and the result of the complex package is in the corresponding phasors basis.

The agreement  is at once expected and also surprising. Since spin is a spectator, we do not expect the spin tune to depend on the amplitude of the spin. This is the result we get.
On the other hand, if one looks at \eq{spinred}, it looks very nonlinear! But since the equation are equivalent (except for a potential sign in front of the square root), we should not expect
any different. The agreement is indeed perfect.

\subsection{\color{.}  The spin invariant  }\label{appspininvariant}
 
With this extended normal form, we expect the spin invariant to be simply a circle transformed back into the original space, that is to say:

{\footnotesize
\begin{verbatim}
1) c_n0%v(1)=(1.0_dp.cmono.1)**2+(1.0_dp.cmono.2)**2
2) c_n0%v(2)=(1.0_dp.cmono.3)**2+(1.0_dp.cmono.4)**2
3) c_n0%v(3)=(1.0_dp.cmono.7)**2+(1.0_dp.cmono.8)**2


   do i=1,3
4)  c_n0%v(i)=c_n0%v(i)*c_n%a_t**(-1)
   enddo
 
\end{verbatim}
}

The first two invariants (lines 1 and 2) are the usual orbital invariants of the $x-p_x$ and  $y-p_y$ planes, which we denote as
${\varepsilon }_{1}$ and ${\varepsilon }_{2}$. 
 We do not bother with the invariant of the modulated magnet because this is simply a circle: the circle of the clock keeping the phase of the modulated magnet
The third plane in the $7^{th}$ and $8^{th}$ dimensions represents the spin. It is simply
%
%]|Expr|[#b @`b___})-# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""Symbol^:"&c0  i:!&c0!*,F,]<c!$1(#$^q^s^2,K$^p^s^2}}:"&c0  .P|
%|$^"#*|:#&c0!*a^t(",M1}:! : ;8/<:!in78: /=;bP8-;|
%|}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \iota &=\left({{q}_{s}^{2}+{p}_{s}^{2}}\right)\circ {a}_{t}^{-1} \label{in78}\end{align}


In the spectator theory, the scalar product of the invariant spin field with spin, $\vec{n}\cdot \vec{s}$, is the spin invariant. It is the cosine of the angle between the spin field and the spin. Obviously the sine itself is also an invariant:
%
%]|Expr|[#b @`b___})0# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""Symbol^:"&c0  s:!&c0!*,F,]"#*|:#1,M$^<c!$1(#<c$%!^n}:"&c0  /7|
%|<c$%!^:#&c0!*s}}}_^2,]:! :#1,M$^<c!$1(($^n^1_$^s^1_,K$^n^3_$^|
%|s^3_,K$^n^2_<b R(%1,M$^s^1^2,M$^s^3^2}_}}}_^2: ;8/<:#;bP8signs|
%|: ;8/=;bP8-;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \sigma &=1-{\left({\vec{n}\cdot \vec{s}}\right)}^{2}=
 1-{\left({{n}_{1}{s}_{1}+{n}_{3}{s}_{3}+{n}_{2}\sqrt {1-{s}_{1}^{2}-{s}_{3}^{2}}}\right)}^{2}\label{signs}\end{align}
This invariant must be closely related to $\iota $ of \eq{in78} which is the circle deformed in  the original variables.

First of all, we notice that the  full map is not symplectic. Therefore there is no reason to believe that the two invariants are the same. For example, if there is a simple dilation of the coordinates, there can be a  multiplicative factor between them. But the situation can be worse, the invariant of \eq{signs} may be a complex function of $\iota $ as well as the orbital invariants. That turns out to be the case.

In the example code, we first compare the quadratic part of \eqs{in78} and \eqe{signs}. Although I allowed a potential dependence on the orbital invariants (something really impossible in leading order), we find out that indeed to quadratic order:
%
%]|Expr|[#b @`b___})0# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|""Symbol^:"&c0  s:!&c0!*,F,], :"&c0  li"#*|:#&c0!*   :"&c0  l|
%|.[:!&c0!*167,N81  : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} \sigma &=\lambda \iota \ \ \ \lambda \approx 167.81  \end{align}
Therefore we can redefine $\iota $. This factor of 167.81 is directly and simply connected to the nonsymplectic nature of the $8 \times 8$ matrix in the extended space.

Next we examine the quartic terms since cubic terms cannot be different at this stage. We attempt a redefinition of $\iota $ based on the most general quartic combination of our extended treatment invariants:
%
%]|Expr|[#b @`b___}):# b'4" Chicago^: ;bP8&c0!*-="!Helvetica|:! |
%|$^""Symbol^:"&c0  i(!"#*|:#&c0!*new}_,]$^v^1_$^:"&c0  i^l^:#&c0!*2|
%|,K$^v^2_$^:"&c0  e^:#&c0!*1^2,K$^v^3_$^:"&c0  e^:#&c0!*2^2,K$|
%|^v^4_$^:"&c0  e^:#&c0!*1_$^:"&c0  e^:#&c0!*2_,K$^v^5_$^:"&c0  e|
%|^:#&c0!*1_$^:"&c0  i^l_:#&c0!*,K$^v^6_$^:"&c0  e^:#&c0!*2_$^:"&c0  i|
%|^l_:!&c0!* : -;}& b!( b"0 b#8 b$@ b%H b&P!WW}]|[
\begin{align} {\iota }_{new}=
{v}_{1}{\iota }_{\lambda }^{2}+{v}_{2}{\varepsilon }_{1}^{2}+{v}_{3}{\varepsilon }_{2}^{2}+{v}_{4}{\varepsilon }_{1}{\varepsilon }_{2}+{v}_{5}{\varepsilon }_{1}{\iota }_{\lambda }+{v}_{6}{\varepsilon }_{2}{\iota }_{\lambda } \end{align}
The variables $v_i$ are close to zero except for $v_5$ and $v_6$.

Upon using the computed value of $v_i$, the new invariant ${\iota }_{new}$ comes exactly equal to the spectator invariant $\sigma $.


In conclusion, a Stern-Gerlach normalisation theory of the spin can be trivially written with a complex FPP if one abandons the Hamiltonian formalism of Yokoya. However we do not recommend it in any standard for several reasons:

\begin{enumerate}
\item The spectator system is good enough.
\item The spectator normal form is fully adapted to the spectator system; the ISF $\vec n$ is directly computed.
\item The simple meat grinder normal form obscures the spectator aspect of the system.
\item We loose one order in the TPSA for no good reason. 
\end{enumerate}

Nevertheless this section illustrates the power of a simple complex FPP.  We can extend the normal form trivially to other systems whose connection to orbital dynamics is absent or very weak.

\chapter{\color{.} Connection between MAD-X and PTC}\label{chapmadptc}

At present there are three codes which are connected to PTC: MAD-X, BMAD and ORBIT.  MAD-X and BMAD are general purpose codes and ORBIT is a space charge code. Although BMAD has by far the best interface to PTC, it is also a code that  Sasha Molodozhentsev and I  have used only at the ``sandbox level.'' So I will restrict my comments to MAD-X with the {\it caveat} that the reader should be aware that BMAD is better connected and that BMAD supports already some complex structures and spin tracking. BMAD supports also a blasphemous interface: using PTC as a tool box. Please read its manual at Cornell if you are interested.

I will not describe how to create inside MAD-X a PTC lattice. Please consult their Web site. I will give an example: it is in the folder {\tt madgino} and the MAD-X file is {\tt als.h}.

The MAD-X file will produce a ``flat'' file called {\tt alsflat.txt} through a ``PTC script''. Access to PTC scripts in MAD-X was programmed by Piotr Skowronski: it is a useful back door which allows me to access PTC's routines which are not in MAD-X. 

\section{\color{.} PTC part of {\tt als.h}}\label{chapmadin}

This is the part of the MAD-X lattice which generates a PTC lattice. The integration is far from perfect but it is very useful to pure PTC users.
The instruction on how to run MAD-X with my graphical interface can be found in the folder madgino in the file {\tt  Instructions.txt}. If you are on another platform,
just run MAD-X as you do normally on the file  {\tt als.h}.

\begin{verbatim}
BEAM, particle=positron, energy=1.5;  
 
use, period=ALS;

ptc_open_gino, command=opengino; ! Commands for Windows GUI #g
ptc_create_universe;  ! #1

ptc_create_layout,model=1,method=6,nst=3;  !,exact;  ! #2

ptc_script, file="flat.txt"; ! back door script for PTC #3

ptc_open_gino, command=mini; ! Commands for Windows GUI #g
ptc_open_gino, command=closegino; ! Commands for Windows GUI #g

stop;
\end{verbatim}



The lines \#g  are routines used in my ``gino'' based graphical interface of PTC. Gino is a commercial product. It is perfectly legal to use my executable but I cannot distribute the tools which allow one to recompile it with the INTEL compiler. If you run a version of MAD-X (or PTC) without Gino, these lines are ignored and you get a warning message.

Line \#1 creates two MAD ``universes'' in PTC. These are used to store lattices: standard ones and complicated ones like recirculators. {\it (MAD-X cannot create true PTC colliders and recirculators. BMAD despite some fundamental differences in philosophy can now do so.).}

Line \#2 actually creates the lattice ALS. It is not an effective way to create a lattice since all magnets are cut by the same integration method. But this is not my problem: expert users of PTC, particularly for long term tracking and space charge, cannot afford such a poor selection of methods and integration steps. 

Line \#3 is a call to the ptc script ``flat.txt'':

\begin{verbatim}
select layout   ! Command selecting the first lattice of the universe M_U
1
print flat file ! Command to print a flat file
 alsflat.txt 
return          ! Command to end the script
\end{verbatim}
 
There are many more commands available in the PTC script. Furthermore, I add commands for my collaborator Sasha (and for others) as the need presents itself.

\section{\color{.}  The actual program radiation.f90}\label{chapradiation}

This is the same program as in \cha{chaprad}. If you run MAD_X in the madgino folder, the flat file "alsflat.txt" will be created.

In the file \htm{radiation.f90}, you will be able to make the selection between a hard-wired lattice and a flat file if the flat file exists in the madgino folder.

I should point out that there is a new format of flat file that fully supports complex topologies, siamese and girders. This will be documented later. Anyway, in case you want to look at flat files produced by MAD-X, please look carefully at the structure of \htm{radiation.f90}. It can be mimicked in your own main program on any platform.

\chapter{\color{.} PTC's new flat files}\label{chapptcflat}

PTC is a library. As such it does not have a great ability at creating simple lattices: codes such as MAD, SAD, BMAD, etc... are all designed to create, fit, change and manipulate lattices. How well they do it is of course a debatable matter. BMAD, at present, can event generate PTC style complex geometries. In the future we even hope that the new MAD code of CERN will handle all of PTC's geometries and more.

But irrespective of that, it is useful once a lattice has been created by these fancy codes, to be able to input it {\it faithfully} in an independent program which is linked to PTC. 


\end{appendices}

\bibliographystyle{prsty}
\bibliography{nlinear}
\endbook



